Borrar filtros
Borrar filtros

How to extract variable from a function file while using ode45?

6 visualizaciones (últimos 30 días)
prabhjeet singh
prabhjeet singh el 20 de Mzo. de 2021
Comentada: Jan el 22 de Mzo. de 2021
x = [0.8 -0.2 1 0 0 0.8 -0.2]';
tspan = [0 20];
[t,x]= ode45(@(t,x) fun(t,x),tspan,x);
function [dot] = fun(t,x)
k1 = 3;
k2 = 3;
k3 = 3;
x1 = x(1);
x2 = x(2);
x3 = x(3);
z2 = x(4);
z3 = x(5);
S1 = x(6);
S2 = x(7);
yr = sin(t);
s1 = x1 - yr;
s2 = x2 - z2;
s3 = x3 - z3;
tau2 = exp(-t) + 0.05;
tau3 = exp(-t) + 0.05;
alpha2 = -k1*s1 - (x1^2 + x2^3 +x3 -x2) + cos(t);
z2dot = (alpha2 - z2)/tau2;
alpha3 = -k2*s2 - (x1^2*x2 + x3^5 - x3) + z2dot;
z3dot = (alpha3 - z3)/tau3;
u = -k3*s3 - x1*x2*x3^2 + z3dot;
x1dot = x1^2 + x2^3 + x3;
x2dot = x1^2*x2 + x3^5;
x3dot = u + x1*x2*x3^2;
S1dot = x1dot - cos(t);
S2dot = x2dot - z2dot;
dot = [x1dot x2dot x3dot z2dot z3dot S1dot S2dot]';
For example, I want to extract variable u from this code, is it possible to do so?
  1 comentario
Jan
Jan el 20 de Mzo. de 2021
Please use the tools for formatting code in the forum. I've done this for you this time.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 20 de Mzo. de 2021
Editada: Jan el 20 de Mzo. de 2021
This question is asked several times per day. I ask MathWorks to include an explanation in the documentation.
Yes, of course it is possible. Simply modify the function to be integrated such, that it accepts vectors as input and use the output of ODE45 as input. There is a tricky need to transpose the inputs:
[t, x] = ode45(@fun, tspan, x); % @fun is faster than @(t,x) fun(t,x)
[~, u] = fun(t.', x.');
function [dot, u] = fun(t, x)
k1 = 3;
k2 = 3;
k3 = 3;
x1 = x(1, :);
x2 = x(2, :);
x3 = x(3, :);
... etc.
% Use elementwise operators: * ==> .* , ^ ==> .^ , / ==> ./
end

Más respuestas (1)

prabhjeet singh
prabhjeet singh el 20 de Mzo. de 2021
Thankyou very much for the instant respone.
I am still not able to plot (t,u). I am expecting response similar to the attachment. Can you please help regarding this. Thanks a ton in advance.
  7 comentarios
prabhjeet singh
prabhjeet singh el 22 de Mzo. de 2021
% Main file
x = [0.8 -0.2 1 0 0 0.8 -0.2]';
tspan = [0 20];
[t,x]= ode45(@fun,tspan,x);
[~ , u] = fun(t.',x.');
yr = sin(t);
figure(1)
plot(t,yr,'--r','Linewidth',1.5);
% Function file
function [dot,u] = fun(t,x)
k1 = 3;
k2 = 3;
k3 = 3;
x1 = x(1);
x2 = x(2);
x3 = x(3);
z2 = x(4);
z3 = x(5);
S1 = x(6);
S2 = x(7);
yr = sin(t);
s1 = x1 - yr;
s2 = x2 - z2;
s3 = x3 - z3;
tau2 = exp(-t) + 0.05;
tau3 = exp(-t) + 0.05;
alpha2 = -k1.*s1 - (x1.^2 + x2.^3 + x3 - x2) + cos(t);
z2dot = (alpha2 - z2)./tau2;
alpha3 = -k2.*s2 - (x1.^2.*x2 + x3.^5 - x3) + z2dot;
z3dot = (alpha3 - z3)./tau3;
u = -k3.*s3 - x1.*x2.*x3.^2 + z3dot;
x1dot = x1.^2 + x2.^3 + x3;
x2dot = x1.^2.*x2 + x3.^5;
x3dot = u + x1.*x2.*x3.^2;
S1dot = x1dot - cos(t);
S2dot = x2dot - z2dot;
dot = [x1dot x2dot x3dot z2dot z3dot S1dot S2dot]';
end
Now if you execute this, I get plot of yr and t. But if I edit code as per your suggestion, I get different plot. Hope it is clear now.
Sorry for bothering you alot for this question.
Jan
Jan el 22 de Mzo. de 2021
"if I edit code as per your suggestion" - this still does not allow to understand, what you are doing to obtain different values for yr. You just show one of the methods, but what is the other?
[~ , u, yr] = fun(t.', x.');
figure;
plot(t, yr);
hold on
plot(t, sin(t), 'ro');
function [dot,u,yr] = fun(t,x)
...
yr = sin(t);
...
end
Except for transposition of the vector both outputs are equal.
You questions about Matlab are welcome in this forum. You do not "bother", but try to solve a problem. This is the purpose of this forum and if I do not find the time to assist you, somebody else will do this.

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by