How can I use funtions defined in a column vector individually?

1 view (last 30 days)
I am looking to solve a problem using ode45 and Runge-Kutta method. For ode45 I hav defined the function as follows.
odefun = @(x,T) [T(2); -a*(Ta-T(1))];
I want to use each of the individual function defined in each row in my Runge-Kutta function. Can I extract them from the odefun above or do I need to define them separately again as two anonymous function?
  2 Comments
Jan
Jan on 11 Jul 2021
What does this mean:
"I want to use each of the individual function defined in each row in my Runge-Kutta function."
If you create your own Runge-Kutta method, prefer to evaluate the function exactly as ODE45 does it. Do not adjust the integrator to the number of elements the function has, because it is much easier to program it generally.
Divyaprakash
Divyaprakash on 11 Jul 2021
Edited: Divyaprakash on 11 Jul 2021
The RK function that I have written takes only one function at a time. If I have two simultanneous firt order ODE, then I define them with two anonymous functions. What I want to do is use the same format of odefun as used in ode45, in my RK4 code as well. But I am unable to figure out how that's done. I went through ode45 implementation in MATLAB, however I am unable to figure it out. Please help me understand how the ode45 routine acceses the rows of odefun individually. My code for RK4 is given below.
Edit: I had posted a different code below. I have changed it.
function [t,y] = RK4(fun,t0,tN,h,y0)
% RK4: Solves IVP using using Runge-Kutta Method
% [t,y] = RK4(fun,t0,tN,h,y0):
% Solves the given IVP using Runge-Kutta fourth order method
% https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
%
% input:
% fun = An anonymous function describing y'(t) = f(t,y)
% t0 = Initial value of t
% y0 = Intial value of y at t0
% tN = Final value of y
% h = Step size
% output:
% t = Values of t
% y = Values of y calculated at all t
%
% Author: Divyaprakash
t = t0:h:tN;
nt = numel(t);
y = zeros(1,nt);
y(1) = y0;
for i = 1:nt-1
k1 = fun(t(i),y(i));
k2 = fun(t(i)+h/2, y(i)+h*k1/2);
k3 = fun(t(i)+h/2, y(i)+h*k2/2);
k4 = fun(t(i)+h,y(i)+h*k3);
y(i+1) = y(i) + 1/6*h*(k1 + 2*k2 + 2*k3 + k4);
end
end

Sign in to comment.

Accepted Answer

Jan
Jan on 11 Jul 2021
A small modification of your code let accept RK4 vector functions also:
function [t, y] = RK4(fun, t0, tN, h, y0)
t = t0:h:tN;
nt = numel(t);
y = zeros(numel(y0), nt);
y(:, 1) = y0;
for i = 1:nt-1
k1 = fun(t(i), y(:, i));
k2 = fun(t(i)+h/2, y(:, i) + h*k1/2);
k3 = fun(t(i)+h/2, y(:, i) + h*k2/2);
k4 = fun(t(i)+h, y(:, i) + h*k3);
y(:, i+1) = y(:, i) + h * (k1 + 2*k2 + 2*k3 + k4) / 6;
end
  4 Comments
Jan
Jan on 13 Jul 2021
@Divyaprakash: All code published in the forum is covered by the CC BY-SA 3.0 license. See: https://creativecommons.org/licenses/by-sa/3.0/
So you can share and adapt the code, as long as you publish your code under the same license and give "approrpiate credit" to the author of the original.
Code published in the FileExchange is covered by the BSD license, see https://www.mathworks.com/matlabcentral/FX_transition_faq.html , to be exact: "3-clause BSD license".
Well. Are the CC BY-SA 3.0 and the 3-clause BSD license compatible? I don't know, but I hope so.
But all I've done is inserting some ":, " in your code. So is my contribution worth to be covered by a license at all?
I'm getting near to desperation with such deliberations. So I prefer to suggest to use any suggestion I gave in the forum however you want. I'm happy, if it helps you. Maybe I should invent a new license "Jan-DWYW-1.0":
% USE THIS CODE HOWEVER YOU WANT BUT DON'T BLAME ME IF IT FAILS.
% MENTION MY NAME ONLY IF YOUR CODE DOES NOT CONTAIN BUGS.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 11 Jul 2021
Please help me understand how the ode45 routine acceses the rows of odefun individually.
It doesn't, not the way I think you mean it.
odefun = @(x,T) [T(2); -a*(Ta-T(1))];
This is not two "individual function[sic] defined in each row in my Runge-Kutta function". This is one anonymous function that accepts two inputs, x and T (which must have at least two elements) and returns a vector as its output. That vector has at least two elements assuming neither a nor Ta were empty, but still just one function.
You could call your odefun with two inputs and then index into the vector that odefun returns, but that does not make odefun two functions in any way, shape, or form.
f = @(x, y) [sind(x); cosd(y)];
z = f(45, 135)
z = 2×1
0.7071 -0.7071
z(2) - cosd(135)
ans = 0
Because of the way f is constructed and called, z(2) is cosd(135).
  1 Comment
Divyaprakash
Divyaprakash on 11 Jul 2021
Okay. So if I want to use the same formulation of odefun as in ode45 in my RK code, then I should just index into the corresponding output as in the code below? Do you recommend some better way or is this fine?
I am using N to index into the output in the code below.
for i = 1:nt-1
k1 = fun(t(i),y(i));
k2 = fun(t(i)+h/2, y(i)+h*k1(N)/2);
k3 = fun(t(i)+h/2, y(i)+h*k2(N)/2);
k4 = fun(t(i)+h,y(i)+h*k3(N));
y(i+1) = y(i) + 1/6*h*(k1(N) + 2*k2(N) + 2*k3(N) + k4(N));
end

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!