Clear Filters
Clear Filters

How to use Runge-Kutta orde 4 in sigmoid equation. I want to plot sigmoid equation with Runge kutta orde 4th but my graph is linear. I need help

3 views (last 30 days)
This is my function
%This is my Runge Kutta for sigmoid equation
S=0;
for j = 1:length(t)-1
k1S = dt*fRK4S(S(j));
k2S = dt*fRK4S(S(j)+k1S/2);
k3S = dt*fRK4S(S(j)+k2S/2);
k4S = dt*fRK4S(S(j)+k3S);
S(j+1) = S(j)+1/6*(k1S+2*k2S+2*k3S+k4S);
end
plot (S)

Accepted Answer

VBBV
VBBV on 26 May 2023
You need to pass both t & S parameters to function fRK4S according to Runge-Kutte 4th order scheme
%This is my Runge Kutta for sigmoid equation
S(1)= 1; dt = 0.5;
a = 0; b = 10;
t = 0:dt:10; N = 100;
h = (b-a)/N;
for j = 1:length(t)-1
k1S = fRK4S(t(j),S(j));
k2S = fRK4S(t(j)+h/2,S(j)+(h*k1S)/2);
k3S = fRK4S(t(j)+h/2,S(j)+(h*k2S)/2);
k4S = fRK4S(t(j)+h, S(j)+h*k3S);
S(j+1) = S(j)+(1/6)*(k1S+2*k2S+2*k3S+k4S);
end
plot(t,S); grid
function S = fRK4S(t,S)
S = S/(1+exp(-t));
end
  5 Comments
Torsten
Torsten on 26 May 2023
Edited: Torsten on 26 May 2023
Did you change your function according to
function dS = fRK4S(t,S)
dS = S/(1+exp(-t));
end
?
What do you get as output if you insert the two lines
size(t)
size(S)
before the plot command ?

Sign in to comment.

More Answers (1)

Torsten
Torsten on 26 May 2023
Why do you use a method for ordinary differential equations to integrate a simple function of the independent variable ?
Use "integral" instead or
syms T
f = 1/(1+exp(-T));
F = int(f,0,T);
F = matlabFunction(F);
T = 0:0.25:10;
plot(T,F(T))

Community Treasure Hunt

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

Start Hunting!