iteration in runge-kutta

hi,
how to calculate (w) for this matrix h=[0.5,0.1,0.7,0.01] in one script and keep data for each (h)
clc;clear all;close all;
h = 0.5;
w(1) = 0.5;
a = 0;
b = 2;
z = a:h:b;
t = zeros(1,length(z));
for i = 1:(length(z)-1)
k1 = h*f(t(i),w(i));
k2 = h*f(t(i) + h/2, w(i) + k1/2);
k3 = h*f(t(i) + h/2, w(i) + k2/2);
k4 = h*f(t(i) + h, w(i) + k3);
w(i+1) = w(i) + ((k1 + 2*k2 + 2*k3 + k4)/6);
t(i+1) = t(i) + h;
end

Answers (1)

KSSV
KSSV on 21 May 2021
Edited: KSSV on 21 May 2021
clc; clear all ;
clc;clear all;close all;
H = [0.5,0.1,0.7,0.01] ;
a = 0;
b = 2;
m = length(H) ;
T = cell(m,1);
W = cell(m,1);
for j = 1:m
h = H(j) ;
z = a:h:b;
n = length(z) ;
w = zeros(1,n) ;
t = zeros(1,n) ;
w(1) = 0.5 ;
for i = 2:n
k1 = h*f(t(i),w(i));
k2 = h*f(t(i) + h/2, w(i) + k1/2);
k3 = h*f(t(i) + h/2, w(i) + k2/2);
k4 = h*f(t(i) + h, w(i) + k3);
w(i+1) = w(i) + ((k1 + 2*k2 + 2*k3 + k4)/6);
t(i+1) = t(i) + h;
end
T{j} = t ;
W{j} = w ;
end

4 Comments

Note that n changes with h ...
@Torsten Yes you are right....edited the answer.
tnx for your answer
but if you run my code for h=0.5 and see (w) matrix, you realize that your answer is too wrong.
Instead of
for i=2:n
use
for i=1:n-1

Sign in to comment.

Categories

Asked:

on 21 May 2021

Commented:

on 21 May 2021

Community Treasure Hunt

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

Start Hunting!