How to extract variable from a function file while using ode45?
Show older comments
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 Comment
Jan
on 20 Mar 2021
Please use the tools for formatting code in the forum. I've done this for you this time.
Accepted Answer
More Answers (1)
prabhjeet singh
on 20 Mar 2021
0 votes
7 Comments
Jan
on 20 Mar 2021
Please explain what "still not able to plot (t,u)" means. I've showed you a method to obtain u. So why ist plut(t,u) not sufficient?
The output will draw exactly what way calculated. If you expect something else, maybe your expectation is not matching the code. But as long as I see the code only, I cannot guess, why you expect something else.
prabhjeet singh
on 20 Mar 2021
When you call fun() with matrices, e.g. x1 will be a vector. Then x1^2 is the multiplication of a [1 x n] with a [1 x n] vector. This is not defined mathematically. Using the elementwise .^ instead squares each element of the vector. Try it:
x = 1:3
x * x % Failing
x^2 % Failing also
x .* x % [1, 4, 9]
"can you please plot t and u and display the image here" - so you ask me to apply all needed changes of the code for the elementwise processing by my own to produce the needed diagram? Is this efficient to let me do the work? If you have done this, you can plot u by your own.

prabhjeet singh
on 21 Mar 2021
Jan
on 21 Mar 2021
The yr function insice the function to be integrated is simply:
yr = sin(t)
So please post your code, because I do not undestand, what differs from what.
prabhjeet singh
on 22 Mar 2021
Jan
on 22 Mar 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.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!