フィルターのクリア

How to plot the y and t?

2 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by