How to get multiple plot for two loops

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Shubham Khatri
Shubham Khatri il 3 Feb 2021
Modificato: Shubham Khatri il 3 Feb 2021
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 Commenti
Bhawna Malik
Bhawna Malik il 3 Feb 2021
Thanks Shubham !!
I have some doubts
1)why r u storing zsol=u(:,2); hsol=u(:,2); they both are same.
2)why plot(A,B)?
3)why there are so many figures to plot?
Shubham Khatri
Shubham Khatri il 3 Feb 2021
Hi Bhawna,
  1. It was a typo.
  2. 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.
  3. 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by