
How to plot a graph of 3 separate lines for the below code
2 ビュー (過去 30 日間)
古いコメントを表示
what changes should be made to the below code to get a graph of 3 seperate lines, im getting 3 lines but at certain points like 15,12 etc, on x axis when zoomed in, the lines merge at that point for the fplot graph of ode but i dont want that to happen, the values at 15 for h=1 line is 7933.4848 and for h=3 line the value is 7933.4551 so i want the graph to be plotted at thos points and not get mergerd with the h=15 line. Thanks
syms t x(t)
ode=diff(x,t)==(13950-x)/18.75
ic=x(0)==560;
gs=dsolve(ode)
ps=dsolve(ode,ic)
figure;
fplot(ps,[0 15])
hold on
grid on;
xlabel('Time (in Minute)')
ylabel('Amount of Salt (in kg)')
title("Graph of Analytical vs Numerical Solutions","Glen Rodrigues (F28)")
syms t x
g=(13950-x)/18.75
f=@(t,x) eval(g);
x0=560;
t0=0;
t=15;
h=1;
t_v=[];
x_v=[];
fprintf('t= %0.4f x= %0.4f \n',t0,x0)
for c=1:15
k1=h*f(t0,x0);
k2=h*f(t0+(h/2),x0+(k1/2));
k3=h*f(t0+(h/2),x0+(k2/2));
k4=h*f(t0+h,x0+k3);
x=x0+(1/6)*(k1+(2*k2)+(2*k3)+k4);
t_v=[t_v,c];
x_v=[x_v,x];
fprintf('t= %0.4f x= %0.4f \n',c,x)
t0=t0+h;
x0=x;
end
plot(t_v, x_v,'--','Color','r');
hold on
syms t x
g=(13950-x)/18.75
f=@(t,x) eval(g);
x0=560;
t0=0;
t1=3;
t=15;
h=3;
t_v=[];
x_v=[];
fprintf('t= %0.4f x= %0.4f \n',t0,x0)
while (t1<=t)
k1=h*f(t0,x0);
k2=h*f(t0+(h/2),x0+(k1/2));
k3=h*f(t0+(h/2),x0+(k2/2));
k4=h*f(t0+h,x0+k3);
x=x0+(1/6)*(k1+(2*k2)+(2*k3)+k4);
t_v=[t_v,t1];
x_v=[x_v,x];
fprintf('t= %0.4f x= %0.4f \n',t1,x)
t1=t1+h;
t0=t0+h;
x0=x;
end
plot(t_v, x_v,'-.','Color','g');
legend('h=15','h=1','h=3')
hold off
xlim([14.990 15])
ylim([7929 7934])
legend("Position", [0.72933,0.14068,0.16179,0.13441])
1 件のコメント
Karl
2023 年 12 月 11 日
I think that you're already plotting three curves, just that it's difficult to distinguish between them, at
, at the scales that you've plotted. If I run your code, and zoom in more, the curves separate.

回答 (1 件)
Avni Agrawal
2023 年 12 月 18 日
Hi Glen,
I understand that you are unable to distinguish between lines at certain plots due to ‘xlim’ and ‘ylim’ values.
You can distinguish the plot clearly by setting ‘xlim’ and ‘ylim’ values as mentioned below:
at t=15 minutes:
xlim([14.99998 15.000005])
ylim([7933.47 7933.49])
at t=12 minutes:
xlim([11.999998 12.000002])
ylim([6889.551 6889.558])
I hope this helps.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!