matlab - 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
解决方案
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
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
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
推荐阅读
- regex - 通配符/正则表达式在snakemake中不起作用?
- html - 如何在加载字体之前使用内联元素设置字体图标的大小
- node.js - 如何弄清楚 node.js 代码是如何运行的?
- drupal - 链接图像并添加资产
- php - 迁移到 PHP 7 后出现不一致的 Memcached PHP 会话读取错误
- php - phpword removeblock - 表仍然存在
- facebook - 在 Facebook 上分享页面而不打开图表
- sql - 如何在 2 个差异表之间进行联合
- flutter - Flutter:如何更改 Appbar 文本的字体?
- wcf - 在 wcf 中将对象作为参数传递时获取空对象