MATLAB Answers

0

Plotting graphs while solving Multiple Differential Equations using ODE45

Asked by Kartikeya Sharma on 13 Jun 2019
Latest activity Edited by madhan ravi
on 13 Jun 2019
I created two different scripts to solve a system of differential Equations, while the code worked perfectly fine and graphs were as I expected, I wasn't able to concatenate the various graphs through the "hold on". Please help me locate the error or I would love a new improved version of this code.
The two scripts are:
***********************************************************************************
****Script 1
function fval=rotationeqn(t,y)
A11=y(1);
A12=y(2);
A22=y(3);
recilamda=1000;
Wi=0;
fval(1,1)=-1*recilamda*(A11-1)+Wi*recilamda*A12;
fval(2,1)=-1*recilamda*A12+Wi*0.5*recilamda*(A22-A11);
fval(3,1)=-1*recilamda*(A22-1)-Wi*recilamda*A12;
hold on
Wi=1;
fval(1,1)=-1*recilamda*(A11-1)+Wi*recilamda*A12;
fval(2,1)=-1*recilamda*A12+Wi*0.5*recilamda*(A22-A11);
fval(3,1)=-1*recilamda*(A22-1)-Wi*recilamda*A12;
hold on
end
**********************************************************************************************
*****script 2
x=pi/6;
yo=[cos(x)*cos(x);sin(x)*cos(x);sin(x)*sin(x)];
tSpan=[0:0.00001:0.01];
[tSol,ySol]=ode45(@(t,y) rotationeqn(t,y),tSpan,yo);
plot(tSol,ySol(:,2),'r');
hold on
********************************************************************************************

  0 Comments

Sign in to comment.

1 Answer

Answer by madhan ravi
on 13 Jun 2019
Edited by madhan ravi
on 13 Jun 2019
 Accepted Answer

Looks like you need to parameterize your function ( lookup doc )while you vary Wi parameter. Loop through Wi and plot the solutions.
x=pi/6;
yo=[cos(x)*cos(x);sin(x)*cos(x);sin(x)*sin(x)];
tSpan=0:0.00001:0.01;
for Wi = [0,1] % varying parameter
[tSol,ySol]=ode45(@(t,y) rotationeqn(t,y,Wi),tSpan,yo);
plot(tSol,ySol(:,2));
hold on
end
function fval=rotationeqn(t,y,Wi)
% ^^ - parameterize
A11=y(1);
A12=y(2);
A22=y(3);
recilamda=1000;
fval(1,1)=-1*recilamda*(A11-1)+Wi*recilamda*A12;
fval(2,1)=-1*recilamda*A12+Wi*0.5*recilamda*(A22-A11);
fval(3,1)=-1*recilamda*(A22-1)-Wi*recilamda*A12;
end

  0 Comments

Sign in to comment.