필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
prabhjeet singh
prabhjeet singh 2021년 3월 20일
댓글: Jan 2021년 3월 22일
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
Jan
Jan 2021년 3월 20일
Please use the tools for formatting code in the forum. I've done this for you this time.

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

채택된 답변

Jan
Jan 2021년 3월 20일
편집: Jan 2021년 3월 20일
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

추가 답변 (1개)

prabhjeet singh
prabhjeet singh 2021년 3월 20일
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
prabhjeet singh
prabhjeet singh 2021년 3월 22일
% 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 2021년 3월 22일
"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.

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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by