首页 > 解决方案 > How to plot multiple graphs for different values of step size in MATLAB?

问题描述

I want to make multiple graphs for the solution of an ODE to examine the effect of step size on converging, how can I adjust the following MATLAB code to have several graphs for several values of 'h' in the same plot?

x(1)=0;
y(1)=2;
z(1)=4;
h=0.2;

for i=1:5
    x(i+1)=i*h;
    ky1=-2*y(i)+5*exp(-x(i));
    kz1=-(1/3)*y(i)*z(i)^2;
    ky2=-2*(y(i)+ky1*h/2)+5*(exp(-(x(i)+h/2)));
    kz2=-(1/3)*(y(i)+ky1*h/2)*(z(i)+kz1*h/2)^2;
    ky3=-2*(y(i)+ky2*h/2)+5*(exp(-(x(i)+h/2)));
    kz3=-(1/3)*(y(i)+ky2*h/2)*(z(i)+kz2*h/2)^2;
    ky4=-2*(y(i)+ky3*h)+5*(exp(-(x(i)+h)));
    kz4=-(1/3)*(y(i)+ky3*h)*(z(i)+kz3*h)^2;
    y(i+1)=y(i)+(ky1+2*ky2+2*ky3+ky4)*h/6;
    z(i+1)=z(i)+(kz1+2*kz2+2*kz3+kz4)*h/6;
end

step = [0:5]';
x_i=x';
y_i=y';
z_i=z';

table(step, x_i, y_i, z_i)

plot(x,y,'--mo');
hold on
plot(x,z,'--ro');

legend('y(x) 4th-order RK method (h=0.2)','z(x) 4th-order RK method (h=0.2)');
xlabel('x')
ylabel('y & z')
grid on

标签: matlab

解决方案


You can add an outer for-loop that traverses through each index in h = [1,0.5,0.2,0.1,0.01]. Here I push all the code that does the calculations to a function called Calculate(). Looping through this function using the values in vector/matrix, h will allow you to get the plots with respect to h.

Full Script: All in the Same Plot

Plotted on a Single Plot

x(1)=0;
y(1)=2;
z(1)=4;
h = [1,0.5,0.2,0.1,0.01];

for Index = 1: length(h)
[x,y,z] = Calculate(x,y,z,h(Index));

plot(x,y,'--mo');
hold on
plot(x,z,'--ro');

end

legend('y(x) 4th-order RK method (h=0.2)','z(x) 4th-order RK method (h=0.2)');
xlabel('x');
ylabel('y & z');
grid on


function [x,y,z] = Calculate(x,y,z,h)

for i=1:5
    x(i+1)=i*h;
    ky1=-2*y(i)+5*exp(-x(i));
    kz1=-(1/3)*y(i)*z(i)^2;
    ky2=-2*(y(i)+ky1*h/2)+5*(exp(-(x(i)+h/2)));
    kz2=-(1/3)*(y(i)+ky1*h/2)*(z(i)+kz1*h/2)^2;
    ky3=-2*(y(i)+ky2*h/2)+5*(exp(-(x(i)+h/2)));
    kz3=-(1/3)*(y(i)+ky2*h/2)*(z(i)+kz2*h/2)^2;
    ky4=-2*(y(i)+ky3*h)+5*(exp(-(x(i)+h)));
    kz4=-(1/3)*(y(i)+ky3*h)*(z(i)+kz3*h)^2;
    y(i+1)=y(i)+(ky1+2*ky2+2*ky3+ky4)*h/6;
    z(i+1)=z(i)+(kz1+2*kz2+2*kz3+kz4)*h/6;
end

step = [0:5]';
x_i=x';
y_i=y';
z_i=z';

table(step, x_i, y_i, z_i)

end

Full Script: In Subplots

Multiple Subplots

x(1)=0;
y(1)=2;
z(1)=4;
h = [1,0.5,0.2,0.1,0.01];

for Index = 1: length(h)
[x,y,z] = Calculate(x,y,z,h(Index));

subplot(length(h),1,Index); plot(x,y,'--mo');
hold on
subplot(length(h),1,Index); plot(x,z,'--ro');
legend(['y(x) 4th-order RK method (h=' num2str(h(Index)) ')'],['z(x) 4th-order RK method (h=' num2str(h(Index)) ')']);
xlabel('x');
ylabel('y & z');
grid on

end

function [x,y,z] = Calculate(x,y,z,h)

for i=1:5
    x(i+1)=i*h;
    ky1=-2*y(i)+5*exp(-x(i));
    kz1=-(1/3)*y(i)*z(i)^2;
    ky2=-2*(y(i)+ky1*h/2)+5*(exp(-(x(i)+h/2)));
    kz2=-(1/3)*(y(i)+ky1*h/2)*(z(i)+kz1*h/2)^2;
    ky3=-2*(y(i)+ky2*h/2)+5*(exp(-(x(i)+h/2)));
    kz3=-(1/3)*(y(i)+ky2*h/2)*(z(i)+kz2*h/2)^2;
    ky4=-2*(y(i)+ky3*h)+5*(exp(-(x(i)+h)));
    kz4=-(1/3)*(y(i)+ky3*h)*(z(i)+kz3*h)^2;
    y(i+1)=y(i)+(ky1+2*ky2+2*ky3+ky4)*h/6;
    z(i+1)=z(i)+(kz1+2*kz2+2*kz3+kz4)*h/6;
end

step = [0:5]';
x_i=x';
y_i=y';
z_i=z';

table(step, x_i, y_i, z_i)

end

Ran using MATLAB R2019b


推荐阅读