Reputation: 11
Given the Cauchy problem
y '= (y-1) ^ 2 (y-2) (y-a) and y(1) = y0;
where a = 0, a = 1 or a = 2:
Write a program that draws the solutions of the problem in different cases. It must comply with the following steps: a) The program will input variable.
b) Divide the viewport into three subplots located in two ... the (two in the first and in the second). Using the command if the first subgraph choose if a = 0, the second if a = 1 and third if a = 2.
c) Create a graph axes fixed size [1, 10] x [-1, 3]. using commands ode45 and plot draw in red fixed points. put axis name and a title graph indicating the case in which we (a = 0, a = 1 or a = 2).
this is my resolution:
function exercise6(a)
disp('partB')
f=inline('(y-1)^2*(y-2)(y-a)','y','a');
if a==0
subplot(2,2,1)
hold on
end
if a==1
subplot(2,2,2)
hold on
end
if a==2
subplot(2,2,3)
hold on
end
disp('partC')
%f=inline('(y-1)^2*(y-2)(y-a)','y','a');
linspace(-1,3,50)
axis([-1,3,1,10])
[t,y]=ode45(inline('(y-1)^2*(y-2)(y-a)','y','a'),'0');
[t2,y2]=ode45(inline('(y-1)^2*(y-2)(y-a)','y','a'),'1');
[t3,y3]=ode45(inline('(y-1)^2*(y-2)(y-a)','y','a'),'2');
plot(t,y,'r')
plot(t2,y2,'r')
plot(t3,y3,'r')
if a ~= 0,1,2
disp('must take values 0,1,2')
And the errors:
Error using inline/feval (line 25) Too many inputs to inline function.
Error in odearguments (line 38) [def_tspan,def_y0,def_options] = feval(ode,[],[],'init',extras{:});
Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in Ejercicio6 (line 23) [t,y]=ode45(inline('(y-1)^2*(y-2)(y-a)','y','a'),'0');
any solution please?
thanks :)
Upvotes: 1
Views: 354
Reputation: 163
I think your problem is only the syntax of solver function. According to manual:
[X,T] = ode45(@F,TimeSpan,Xo,[Options,P1,P2,P3...])
Try switching
ode45(inline('(y-1)^2*(y-2)(y-a)','y','a'),'2');
to:
ode45(inline('(y-1)^2*(y-2)(y-a)','t','y','a'),[t0 tf],y0,,2);
where: t0 = start time, tf = end time and y0 = y initial value.
The problem you get is because the input function of the solver must have only 2 inputs (time and current y value). Additional inputs are accepted if you state them at the end of the ODE45 command. (in this case, the additionial input is 'a' which is defined on the last argument of ODE45. The empty argument is the 'OPTIONS' input which can be left blank.
Read: http://www.its.caltech.edu/~ae121/ae121/ode45_Ref2.pdf for more information.
Upvotes: 1
Reputation: 21563
There are multiple error lines shown, but it all comes down to the top one. You created an invalid inline statement.
If you want to fix it this is where you need to search:
inline('(y-1)^2*(y-2)(y-a)','y','a')
I don't know inline, but I guess that the y
and a
do not belong inside the inline statement.
Upvotes: 1