How to plot for a different initial values at same time

10 次查看(过去 30 天)
I need help how to plot for a different initial values of v0 at same time.
if
zspan=[0,400];
v0=[1;0.01;0.12;]; % Initial values
% Creating a matrix
zsol = [];
v1sol = [];
v2sol = [];
v3sol = [];
options=odeset('RelTol',1e-2,'AbsTol',1e-6,'Events',@events);
[t,v] = ode45(@(t,v)rhs(t,v), zspan, v0,options);
zsol = [zsol;t];
v1sol = [v1sol;v(:,1)];
v2sol = [v2sol;v(:,2)];
v3sol = [v3sol;v(:,3)];
figure(1)
plot(abs(v1sol),zsol,'r')
xlabel('Volume Flux, V')
ylabel('Height, z')
grid on ;
figure (2)
plot(abs(v2sol),zsol,'g')
xlabel('Momentum Flux, M')
ylabel('Height, z')
grid on ;
function [rho, Nsqr]=density1(z)
%%Data for density with respect to depth
zval= [2 3 5 7 10 15 20 25 30 40 50 60 70 80 90 100 125 150 160 175 200 225 250 275 300 325 350 375 400];
rho1 = [17.2731684 17.1649375 21.43455647 22.4140625 23.86332207 24.3746967 24.70487685 24.6003125 24.8933125 25.42772826 26.03220776 26.439625 26.8151875 26.86830797 27.1949375 27.34406944 27.5551875 27.728625 27.23423729 27.88542857 27.752249049 28.1025 28.2415 28.37 28.05366667 28.6565 28.7755 28.898 29.013];
rho0=29;
g=9.8;
zvala=400-zval;
zvalue=fliplr(zvala);
rho2=fliplr(rho1);
rho3=smoothdata(rho2,'lowess',6);
rho=interp1(zvalue,rho3,z);
rho4=interp1(zvalue,rho3,z+0.1);
Nsqr=(-g./rho0).*(rho4-rho);
end
function parameters=rhs(z,v)
[~,Nsqr]=density1(z);
alpha=0.116;
dV= 2*alpha*sqrt(v(2));
dM= (v(1).*v(3))./v(2);
dF= -Nsqr.*v(1);
parameters=[dV;dM;dF];
end
function [value, terminate, direction] = events(t,v)
value = v(1);
terminate=1;
direction=0;
end
end

采纳的回答

Torsten
Torsten 2018-1-30
编辑:Torsten 2018-1-30
zspan=[0 400];
v0mat = [1 0.01 0.12;2 0.01 0.12;3 0.01 0.12;4 0.01 0.12];
options=odeset('RelTol',1e-2,'AbsTol',1e-6,'Events',@events);
for k=1:size(v0mat,1)
v0=v0mat(k,:);
[z,v]=ode45(@rhs,zspan,v0,options);
zsol{k}=z;
v1sol{k}=v(:,1);
v2sol{k}=v(:,2);
v3sol{k}=v(:,3);
end
If the assignment of the results to cell works this way (?), you can now plot the results.
Best wishes
Torsten.
  1 个评论
Dereje
Dereje 2018-1-30
Hi Torsten, Yes it works I've just put the plot inside the loop.
hold on
figure(1)
for k=1:size(v0mat,1)
plot(v1sol{k},zsol{k})
end
Thanks. You are always helpful!!

请先登录,再进行评论。

更多回答(1 个)

Morteza Hajitabar Firuzjaei
you can name both plot as figure(1)
Morteza Hajitabar Firuzjaei
  5 个评论
Torsten
Torsten 2018-1-30
Call ode45 in a loop, save the results from each run in a cell matrix and plot the results after MATLAB exited the loop.
Best wishes
Torsten.
Dereje
Dereje 2018-1-30
I am getting errors. I defined it like you said. Is there any thing wrong with the initialization?
zspan=[0,400];
v0=[1:4;0.01;0.12;]; % Initial values
% Creating a matrix
N=5;
zsol = [];
v1sol = [];
v2sol = [];
v3sol = [];
options=odeset('RelTol',1e-2,'AbsTol',1e-6,'Events',@events);
[t,v] = ode45(@(t,v)rhs(t,v), zspan, v0(1:N),options);
for k=1:length(v0)
[tN,vn] = ode45(@(t,v)rhs(t,v), zspan, v0(1:k),options);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by