iteration in runge-kutta
Show older comments
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)
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
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!