How to plot the y and t?

1 次查看(过去 30 天)
ken
ken 2022-4-9
回答: KSSV 2022-4-9
a = 0;
b = 10;
h = 10^-3;
N = (b-a)/h;
alpha = 1;
f = @(x,y) 1/y^3;
function [y,t] = rk4(f,a,b,alpha,N)
t = [];
y = [];
y0 = alpha;
for i = 1:N
t_i = a+(i-1)*h;
t = [t t_i];
y = [y y0];
k1 = f(t_i, y0);
k2 = f(t_i + h/2, y0+h/2 * k1);
k3 = f(t_i + h/2, y0+h/2 * k2);
k4 = f(t_i + h, y0+h * k3);
y_ = y0 + h / 6 * (k1+2*k2+2*k3+k4);
y0 = y_;
end
y = [y y0];
t = [t a+N*h];
end

采纳的回答

KSSV
KSSV 2022-4-9
You need to call the function. Provide the input and call the function. There is small typo in your function; it seems the input should be h and not b.
a = 0;
b = 10;
h = 10^-3;
N = (b-a)/h;
alpha = 1;
f = @(x,y) 1/y^3;
[y,t] = rk4(f,a,h,alpha,N) ;
plot(t,y)
function [y,t] = rk4(f,a,h,alpha,N)
t = [];
y = [];
y0 = alpha;
for i = 1:N
t_i = a+(i-1)*h;
t = [t t_i];
y = [y y0];
k1 = f(t_i, y0);
k2 = f(t_i + h/2, y0+h/2 * k1);
k3 = f(t_i + h/2, y0+h/2 * k2);
k4 = f(t_i + h, y0+h * k3);
y_ = y0 + h / 6 * (k1+2*k2+2*k3+k4);
y0 = y_;
end
y = [y y0];
t = [t a+N*h];
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by