How to plot the graph for the given RK 4th order step method

syms t x
g=(13950-x)/18.75
g = 
f=@(t,x) eval(g);
x0=560;
t0=0;
t=15;
h=1;
for c=1:15
k1=h*f(t0,x0);
k2=h*f(t0+(h/2),x0+(k1/2));
k3=h*f(t0+(h/2),x0+(k2/2));
k4=h*f(t0+h,x0+k3);
x=x0+(1/6)*(k1+(2*k2)+(2*k3)+k4);
fprintf('x= %0.4f \n',x)
t0=t0+h;
x0=x;
end
x= 1255.4238 x= 1914.7301 x= 2539.7945 x= 3132.3956 x= 3694.2194 x= 4226.8642 x= 4731.8455 x= 5210.6001 x= 5664.4901 x= 6094.8068 x= 6502.7746 x= 6889.5541 x= 7256.2458 x= 7603.8931 x= 7933.4848

Answers (1)

f=@(t,x)(13950-x)/18.75;
x0=560;
t0=0;
t=15;
h=1;
X(1) = x0;
T(1) = t0;
for c=1:15
k1=h*f(t0,x0);
k2=h*f(t0+(h/2),x0+(k1/2));
k3=h*f(t0+(h/2),x0+(k2/2));
k4=h*f(t0+h,x0+k3);
x=x0+(1/6)*(k1+(2*k2)+(2*k3)+k4);
%fprintf('x= %0.4f \n',x)
t0=t0+h;
x0=x;
X(c+1) = x0;
T(c+1) = t0;
end
hold on
plot(T,X)
syms x(t) t
eqn = diff(x,t) == (13950-x)/18.75;
cond = x(0) == 560;
solx = dsolve(eqn,cond)
solx = 
fplot(solx,[0 15])
hold off
grid on

Categories

Find more on Networks in Help Center and File Exchange

Asked:

on 9 Dec 2023

Edited:

on 9 Dec 2023

Community Treasure Hunt

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

Start Hunting!