How to plot a graph of 3 separate lines for the below code

1 Ansicht (letzte 30 Tage)
Glen Andrew
Glen Andrew am 11 Dez. 2023
Beantwortet: Avni Agrawal am 18 Dez. 2023
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
ode(t) = 
ic=x(0)==560;
gs=dsolve(ode)
gs = 
ps=dsolve(ode,ic)
ps = 
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
g = 
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)
t= 0.0000 x= 560.0000
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
t= 1.0000 x= 1255.4238 t= 2.0000 x= 1914.7301 t= 3.0000 x= 2539.7945 t= 4.0000 x= 3132.3956 t= 5.0000 x= 3694.2194 t= 6.0000 x= 4226.8642 t= 7.0000 x= 4731.8455 t= 8.0000 x= 5210.6001 t= 9.0000 x= 5664.4901 t= 10.0000 x= 6094.8068 t= 11.0000 x= 6502.7746 t= 12.0000 x= 6889.5541 t= 13.0000 x= 7256.2458 t= 14.0000 x= 7603.8931 t= 15.0000 x= 7933.4848
plot(t_v, x_v,'--','Color','r');
hold on
syms t x
g=(13950-x)/18.75
g = 
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)
t= 0.0000 x= 560.0000
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
t= 3.0000 x= 2539.7833 t= 6.0000 x= 4226.8450 t= 9.0000 x= 5664.4656 t= 12.0000 x= 6889.5262 t= 15.0000 x= 7933.4551
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 Kommentar
Karl
Karl am 11 Dez. 2023
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Avni Agrawal
Avni Agrawal am 18 Dez. 2023
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.

Kategorien

Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by