Plotting ODE solutions for a single time point

1 次查看(过去 30 天)
I have plotted the solution to four odes against time.
I now want to plot results for a single time point. Such as for when t=5, i want to plot c_{j}(5) against j. Is there a short command that can do this using the code i have written
function f = beckerdorin(t,C)
a=0.5;
b=1.5;
sigma=0.5;
f(1,1)= b*(C(4)+ C(3)+ 2*C(2))-a*C(1)*(2*C(1)+C(2)+C(3));
f(2,1)= a*(C(1))^2 - b*C(2) - a*C(1)*C(2) + b*C(3)-2*sigma*(a*C(2)^2-b*C(4));
f(3,1)= a*C(2)*C(1)-b*C(3)-a*C(3)*C(1)+b*C(4);
f(4,1)= a*C(3)*C(1)-b*C(4)+sigma*(a*C(2)^2-b*C(4));
end
Plotting
clf
[tv,c] = ode45('beckerdorin',[0,3],[10,0,0,0]);
figure(2)
plot(tv,c(:,1),'r');hold on
plot(tv,c(:,2),'b');hold on
plot(tv,c(:,3),'y');hold on
plot(tv,c(:,4),'g');
legend('C(1)','C(2)','C(3)','C(4)')
xlabel('Time t')
ylabel('c_{j}(t)')

回答(1 个)

Star Strider
Star Strider 2018-9-11
It requires that you slightly change your ode45 call, then use the solution structure with the odextend (link) function:
sln = ode45(@beckerdorin,[0,3],[10,0,0,0])
tv = sln.x';
c = sln.y';
figure(2)
plot(tv,c(:,1),'r');hold on
plot(tv,c(:,2),'b');hold on
plot(tv,c(:,3),'y');hold on
plot(tv,c(:,4),'g');
legend('C(1)','C(2)','C(3)','C(4)')
xlabel('Time t')
ylabel('c_{j}(t)')
slnxtd = odextend(sln, @beckerdorin, 5); % Extend Integration To ‘t=5’
c_5 = slnxtd.y(:,end) % Values Of ‘c’ At ‘t=5’
c_5 =
2.02359968542774
1.3649852291002
0.920727887972237
0.621061548113791
  2 个评论
Aishah Malek
Aishah Malek 2018-9-11
Thankyou, but what if i want to change my j value ,i get the same results when i try another value for j, and also if i want to plot different j values on the same plot.
Star Strider
Star Strider 2018-9-11
My pleasure.
What are you referring to? I do not see ‘j’ defined anywhere in your code. I have no idea what it is. It only appears as part of a character vector in your ylabel call.
You asked how to evaluate your ‘beckerdorin’ function at ‘t=5’, and I provided a way to do that.
I leave the rest to you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by