I am not getting graph for RK4

clear t %Clear old steps
clear y %Clear y values from previous runs
t1=0; % Boundary value 1
t2=10;% Boundary value 2
h=0.1
N=(t2-t1)/h; % Number of steps
y10=1;
y20=0;
y30=0;
%y0=100; % Boundary value of y(a)
%Incremental step size
t(1) = t1; % assign initial location
Y1(1) = y10;
Y2(1) = y20;
Y3(1) = y30;% assign boundary condition
for n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fgas(t(n),y1(n)); %Calls the function f(t,y) = dy/dt
k2 = fgas(t(n)+(h/2),y1(n)+(h/2)*k1);
k3 = fgas(t(n)+(h/2),y1(n)+(h/2)*k2);
k4 = fgas(t(n)+(h/2),y1(n)+(h/2)*k3);
y1(n+1)=y1(n)+h*((k1/6)+(k2/3)+(k3/3)+(k4/6))
end
plot(t,Y1)
hold on
%title(['RK4 N=',num2str(N)])

9 Comments

In the first place is code running?
No it will show error.....you have to repalce Y with y. To help more, you need to show us what is function fgas.
function fgas=fgas(t,y10)
fgas=-1*y10*y10;
you were correct
can you tell me one more thing?
What?
clear t %Clear old steps
clear y %Clear y values from previous runs
t1=0; % Boundary value 1
t2=10;% Boundary value 2
h=0.1
N=(t2-t1)/h; % Number of steps
y10=1;
y20=0;
y30=0;
%y0=100; % Boundary value of y(a)
%Incremental step size
t(1) = t1; % assign initial location
Y1(1) = y10;
Y2(1) = y20;
%Y3(1) = y30;% assign boundary condition
for n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fgas(t(n),y10(n)); %Calls the function f(t,y) = dy/dt
k2 = fgas(t(n)+(h/2),y10(n)+(h/2)*k1);
k3 = fgas(t(n)+(h/2),y10(n)+(h/2)*k2);
k4 = fgas(t(n)+(h/2),y10(n)+(h/2)*k3);
y10(n+1)=y10(n)+h*((k1/6)+(k2/3)+(k3/3)+(k4/6))
n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fcra(t(n),y20(n)); %Calls the function f(t,y) = dy/dt
k2 = fcra(t(n)+(h/2),y20(n)+(h/2)*k10);
k3 = fcra(t(n)+(h/2),y20(n)+(h/2)*k20);
k4 = fcra(t(n)+(h/2),y20(n)+(h/2)*k30);
y20(n+1)=y20(n)+h*((k10/6)+(k20/3)+(k30/3)+(k40/6))
end
plot(t,y10)
hold on
plot(t,Y2)
hold on
i am adding another function
and getting error in the n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h; line
before fcra

Sign in to comment.

Answers (2)

Alan Stevens
Alan Stevens on 18 Oct 2020
Looks like you are confusing Y with y.

7 Comments

did not get you
In these lines
k1 = fgas(t(n),y1(n)); %Calls the function f(t,y) = dy/dt
k2 = fgas(t(n)+(h/2),y1(n)+(h/2)*k1);
k3 = fgas(t(n)+(h/2),y1(n)+(h/2)*k2);
k4 = fgas(t(n)+(h/2),y1(n)+(h/2)*k3);
y1(n+1)=y1(n)+h*((k1/6)+(k2/3)+(k3/3)+(k4/6))
You have lower case y1. I suspect they should be upper case Y1.
clear t %Clear old steps
clear y %Clear y values from previous runs
t1=0; % Boundary value 1
t2=10;% Boundary value 2
h=0.1
N=(t2-t1)/h; % Number of steps
y10=1;
y20=0;
y30=0;
%y0=100; % Boundary value of y(a)
%Incremental step size
t(1) = t1; % assign initial location
Y1(1) = y10;
Y2(1) = y20;
%Y3(1) = y30;% assign boundary condition
for n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fgas(t(n),y10(n)); %Calls the function f(t,y) = dy/dt
k2 = fgas(t(n)+(h/2),y10(n)+(h/2)*k1);
k3 = fgas(t(n)+(h/2),y10(n)+(h/2)*k2);
k4 = fgas(t(n)+(h/2),y10(n)+(h/2)*k3);
y10(n+1)=y10(n)+h*((k1/6)+(k2/3)+(k3/3)+(k4/6))
n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fcra(t(n),y20(n)); %Calls the function f(t,y) = dy/dt
k2 = fcra(t(n)+(h/2),y20(n)+(h/2)*k10);
k3 = fcra(t(n)+(h/2),y20(n)+(h/2)*k20);
k4 = fcra(t(n)+(h/2),y20(n)+(h/2)*k30);
y20(n+1)=y20(n)+h*((k10/6)+(k20/3)+(k30/3)+(k40/6))
end
plot(t,y10)
hold on
plot(t,Y2)
hold on
i want to add another function
and i am getting error for second function
why is that so?
You need to show what function fcra is.
function fcra=fcra(t1,y10,y20)
fcra=((-1*y10*y10)-(0.5*y20));
Index exceeds the number of array elements (2).
Error in RungeKutta2 (line 26)
t(n+1) = t(n)+h;

Sign in to comment.

Parth Shah
Parth Shah on 18 Oct 2020
function fcra=fcra(t1,y10,y20)
fcra=((-1*y10*y10)-(0.5*y20));

10 Comments

Index exceeds the number of array elements (2).
Error in RungeKutta2 (line 26)
t(n+1) = t(n)+h;
Alan Stevens
Alan Stevens on 18 Oct 2020
Edited: Alan Stevens on 18 Oct 2020
You have given functio fcra three arguments, but you are only calling it with two.
And you don't need to redefine n = 1:N and t(n+1) within your first for n =1:N loop.
okay so what should i write for k1?
k1 = fcra(t(n),y20(n),y10(n)); %Calls the function f(t,y) = dy/dt
k2 = fcra(t(n)+(h/2),y20(n)+(h/2)*k10),y10(n)+(h/2)*k1)
like this way?
The following works, but you'll need to check carefully that it does what you want:
t1=0; % Boundary value 1
t2=10;% Boundary value 2
h=0.1;
N=(t2-t1)/h; % Number of steps
y10=1;
y20=0;
y30=0;
%y0=100; % Boundary value of y(a)
%Incremental step size
t(1) = t1; % assign initial location
Y1(1) = y10;
Y2(1) = y20;
%Y3(1) = y30;% assign boundary condition
for n=1:N % For loop, sets next t,y values
t(n+1) = t(n)+h;
k1 = fgas(t(n),y10(n)); %Calls the function f(t,y) = dy/dt
k2 = fgas(t(n)+(h/2),y10(n)+(h/2)*k1);
k3 = fgas(t(n)+(h/2),y10(n)+(h/2)*k2);
k4 = fgas(t(n)+(h/2),y10(n)+(h/2)*k3);
y10(n+1)=y10(n)+h*((k1/6)+(k2/3)+(k3/3)+(k4/6));
%n=1:N % For loop, sets next t,y values
%t(n+1) = t(n)+h;
y10a = y10(n);
y10b = y10(n)+(h/2)*k1;
y10c = y10(n)+(h/2)*k2;
y10d = y10(n)+(h/2)*k3;
k10 = fcra(t(n),y20(n),y10a); %Calls the function f(t,y) = dy/dt
k20 = fcra(t(n)+(h/2),y20(n)+(h/2)*k10,y10b);
k30 = fcra(t(n)+(h/2),y20(n)+(h/2)*k20,y10c);
k40 = fcra(t(n)+(h/2),y20(n)+(h/2)*k30,y10d);
y20(n+1)=y20(n)+h*((k10/6)+(k20/3)+(k30/3)+(k40/6));
%
end
plot(t,y10)
hold on
plot(t,y20)
function fg = fgas(~,y10)
fg = -1*y10*y10;
end
function fc=fcra(~,y20,y10)
fc=((-1*y10*y10)-(0.5*y20));
end
graph is very weird
Possibly so, but it looks like you are trying to solve the following equations:
% dy1/dt = -y1^2 y1(0) = 1
% dy2/dt = -y1^2 - 0.5*y2 y2(0) = 0
If so, using Matlab's ode45 solver we can do the following:
y0 = [1 0];
tspan = [0 10];
[t, y] = ode45(@odefn, tspan, y0);
plot(t,y)
function dydt = odefn(~,y)
dydt = [-y(1)^2;
-y(1)^2 - 0.5*y(2)];
end
This results in the same graph:
If you are trying to solve a different set of equations then your code isn't yet finished!
I WNAT THIS GRAPH BUT I WANTED TO DO BY RK4 METHOD
That's exactly what your code does then!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 18 Oct 2020

Commented:

on 18 Oct 2020

Community Treasure Hunt

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

Start Hunting!