I am trying to solve this assignment for my Differential Equations class and I am encountering some problems.
Given this system of ODEs:
dy(1) = y(2) − y(3);
dy(2) = −3*y(2) − 5*sin(ω*y(1));
dy(3) = y(1)*y(2);
with omega=3
. Initial values: y(1)=0; y(2)=4; y(3)=1;
Plot f
between t=0
and t=20
:
f(t) = |2*cos(y1(t)) + y2(t)|
I have first tried simulating the equation system with ODE45 with this code:
On editor:
function dy=modelo10(t,y)
global omega
dy = zeros(3,1);
dy(1)= y(2) - y(3);
dy(2)= -3*y(2) - 5*sin(omega*y(1));
dy(3)= y(1)*y(2);
end
On command tab:
>>global omega;
>>omega = 3;
>>[T,Y] = ode45(@modelo10, [0,20], [0,4,1]);
%% I assign the function to variable f
>>f = @(t) 2.*cos(y(:,1)) + y(:,2);
%% And finally plot it with the values t
>>fplot(f, [0,20]);
I get a blank graph with the following error:
Warning: Function behaves unexpectedly on array inputs. To improve
performance, properly
vectorize your function to return an output with the same size and
shape as the input
arguments.
In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 241)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 196)
In fplot>vectorizeFplot (line 196)
In fplot (line 166)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in
FunctionLine update:
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in
FunctionLine update:
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false
So my question is what are the steps (or commands) to plot f
?
I am using MATLAB R2019a.