Differential Equations MATLAB issue
조회 수: 6 (최근 30일)
이전 댓글 표시
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)));
댓글 수: 0
답변 (1개)
Alan Stevens
2021년 3월 19일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
