How can I use funtions defined in a column vector individually?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Divyaprakash
el 10 de Jul. de 2021
Comentada: Divyaprakash
el 15 de Jul. de 2021
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 comentarios
Jan
el 11 de Jul. de 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
el 11 de Jul. de 2021
Editada: Divyaprakash
el 11 de Jul. de 2021
Respuesta aceptada
Jan
el 11 de Jul. de 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 comentarios
Jan
el 13 de Jul. de 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.
Más respuestas (1)
Steven Lord
el 11 de Jul. de 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) - cosd(135)
Because of the way f is constructed and called, z(2) is cosd(135).
1 comentario
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!