Plotting graphs while solving Multiple Differential Equations using ODE45

9 views (last 30 days)
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
********************************************************************************************

Accepted Answer

madhan ravi
madhan ravi on 13 Jun 2019
Edited: madhan ravi on 13 Jun 2019
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

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!