Reputation:
function yp = nonlinear (t,y)
e=0.2;
yp(1)=y(2);
yp(2) = (-y(1)-e*y(1)^3);
tspan = [0.20];
y0=[0;0]
[t,y]=ode45('nonlinear',tpsan,y0)
plot (t,y(:,1))
grid
xlabel('time')
ylabel('u')
title ('u vs. t')
hold on;
Sorry I am absolutely noob at matlab, when I try to execute the code, it says "undefined function of variable t". I am trying to use ode45 to solve a differential equation
Upvotes: 0
Views: 208
Reputation: 13876
Read the documentation on ode45
. You need to save the following lines of your code into a file nonlinear.m
function yp = nonlinear (t,y)
e=0.2;
yp(1)=y(2);
yp(2) = (-y(1)-e*y(1)^3);
and then in a separate (script) file, save the rest of your code:
tspan = [0.20];
y0=[0;0]
[t,y]=ode45('nonlinear',tpsan,y0)
plot (t,y(:,1))
grid
xlabel('time')
ylabel('u')
title ('u vs. t')
hold on;
You might also want to familiarise yourself with MATLAB before attempting that sort of thing. Check out the tutorials.
Upvotes: 1