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

조회 수: 2 (최근 30일)
Divyaprakash
Divyaprakash 2021년 7월 10일
댓글: Divyaprakash 2021년 7월 15일
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
Jan
Jan 2021년 7월 11일
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 2021년 7월 11일
편집: Divyaprakash 2021년 7월 11일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 7월 11일
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
Jan
Jan 2021년 7월 13일
@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.
Divyaprakash
Divyaprakash 2021년 7월 15일
Thanks. I was unaware of these license requirements.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 7월 11일
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
Divyaprakash
Divyaprakash 2021년 7월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by