Differential Equations MATLAB issue

6 visualizaciones (últimos 30 días)
Hannah Pike
Hannah Pike el 19 de Mzo. de 2021
Comentada: darova el 20 de Mzo. de 2021
So for my Differential Equations class, we have a project about a spring. It provides and equation, and an initial condition, and asks to make a plot of it. To solve this through code, I had to make one function and one main code. However, when I run my code, I get an error message that says ...
" Error using odearguments (line 93)
NONLINEAR must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Project1_Main (line 6)
[t,y] = ode45('nonlinear', tspan, y);"
The weird thing about this error is that I have no where near 115 lines of code (l literally have like 8 lines), and NONLINEAR is not a vector its the name of my function.
Please help me fix my code so that it operates. I will post the question I am solving and my code, hopefully somone can help me find my mistake.
Problem:
Suppose a nonlinear spring-mass system satisfies the initial value problem ( u 00 + u + Eu3 = 0 u(0) = 0, u0 (0) = 1
Use ode45 and plot to answer the following: 1. Let E = 0.0, 0.2, 0.4, 0.6, 0.8, 1.0 and plot the solutions of the above initial value problem for 0 ≤ t ≤ 20. Estimate the amplitude of the spring.
My Main Code:
tspan = [0 20];
y = [0 ; 0];
[t,y] = ode45('nonlinear', tspan, y);
plot(t,y(:,1))
grid
xlabel("time")
ylabel("u")
title("u vs v")
hold on
My Function Code:
function yp = nonlinear(t, y)
e = 0.2;
y(1)= y(2);
yp(2) = (-y(1) - e ^ (t * (y(1) ^ 3)));

Respuestas (1)

Alan Stevens
Alan Stevens el 19 de Mzo. de 2021
Make sure your function 'nonlinear' returns a column vector. Your have initial conditions as [0, 0]. This should be either [0, 1] (as used below) or [1, 0], I think.
tspan = [0 20];
y = [0 ; 1];
[t,y] = ode45(@nonlinear, tspan, y); % Call the function like this
plot(t,y(:,1))
grid
xlabel("time")
ylabel("u")
title("u vs v") % You are plotting u vs time not u vs v
hold on
%My Function Code:
function yp = nonlinear(t, y) % Must return a column vector
e = 0.2;
yp = [y(2);
(-y(1) - e ^ (t * (y(1) ^ 3)))];
end
  2 comentarios
Hannah Pike
Hannah Pike el 19 de Mzo. de 2021
Thank you! That helped!
darova
darova el 20 de Mzo. de 2021

Iniciar sesión para comentar.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by