How to get multiple plot for two loops
2 次查看(过去 30 天)
显示 更早的评论
I have a code where I run two loops . one for s1 and other for s2. For each s1 and s2. I get a solution for my variable say z and h and i store it in a matrix. Now i need to plot this . Say I run two loops with 10 , 10 gaps then i get z as 10by 10 matrix and h as 10 by 10 matrix . So I need multiple plots where X axis is z and y is h.
I am pasting the Main file of my code.
% time series simulation for entire linear model
%-----------------------------------------------------------------------------------
tic
clear
%close
T=2000;
tau = [0,T]; %time interval
s1=linspace(0,20,10);
s2=linspace(0.2,0.7,10);
for i=1:length(s1)
for j=1:length(s2)
u0 = [0.70 0.25 0.0 0.0 0.5 0.05 2]; %initial condition
para =parametersurf(s1(i),s2(j));
opts = odeset('RelTol', 2.22045e-14,'AbsTol', 2.22045e-14);
[t,uu] = ode45(@modelsurf,tau,u0,opts,para);
Z1=0:1:T;
u=interp1(t,uu,Z1,'spline');
ssol=u(:,1);
ysol=u(:,2);
y_ssol=u(:,3);
y_tsol=u(:,4);
fssol=u(:,5);
zsol=u(:,6);
hsol=u(:,7);
A(i,j)=zsol(end);
B(i,j)=hsol(end);
end
end
plot(A,B)
hold on
toc
By putting command for plot I get single line but I should multiple plots . For i=1 , i should get one 10 points (as j=10). Then i=2(j=10)....................like this
0 个评论
采纳的回答
Shubham Khatri
2021-2-3
编辑:Shubham Khatri
2021-2-3
Hello,
To my understanding, you are running loop twice and assigning values to the matrix z and h. Please check, that the dimension of z and h obtained after running the code is of 10x10. If you are getting a single line that means that they both have a dimension of 1x10. I am attaching a sample code for better understanding. For more information on various functions used, please refer to the documentation link of ode45 , plot and for loop
% time series simulation for entire linear model
%-----------------------------------------------------------------------------------
tic
clc
clear all
%close
T=2000;
tau = [0,T]; %time interval
s1=linspace(0,20,2); %reducing 10 to 2 to reduce computational load
s2=linspace(0.2,0.7,2);
for i=1:length(s1)
for j=1:length(s2)
u0 = [0.70 0.25 0.0 0.0 0.5 0.05 2]; %initial condition
opts = odeset('RelTol', 2.22045e-14,'AbsTol', 2.22045e-14);
yprime = @(t,uu) -2*uu + 2*cos(t).*sin(2*t); %modified sample function
[t,uu] = ode45(yprime,tau,u0,opts); %modified sample conditions
Z1=0:1:T;
u=interp1(t,uu,Z1,'spline');
zsol=u(:,1);
hsol=u(:,2);
A(i,j)=zsol(end);
B(i,j)=hsol(end);
z1(i,j)=i+j; %sample variables for better understanding
z2(j)=i+2; %sample variables for better understanding
h1(i,j)=j-i; %sample variables for better understanding
end
h2(i)=i-2; %sample variables for better understanding
end
plot(A,B)
toc
figure
plot(z1,h1)
figure
plot(z1,h2)
figure
plot(z2,h1)
figure
plot(z2,h2)
Hope it helps
2 个评论
Shubham Khatri
2021-2-3
Hi Bhawna,
- It was a typo.
- Plot(A,B) because we are storing results in A and B and as per your description you want Z values in X axis and H vlaues in Y axis.
- The plot figures are explaing various cases where z1,z2,h1 and h2 have different dimensions, so what will be its impact on plots. I have created one plot for each case for better understanding.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!