How to input discrete values describing state into an equation?
Show older comments
I have discrete pairs of points [t,x] where x = f(x1(t),x2(t),x3(t),x4(t)).
I also have a function E = f(x1(t),x2(t),x3(t),x4(t)).
I want to plot E vs. time (t). I don't have functions for x1(t), x2(t), etc., only the discrete points [t,x]. How can I plot E vs. t?
2 Comments
Star Strider
on 21 Sep 2014
Is x (or f) a matrix with discrete values of x1...x4 in one column defined at discrete values of t in another column? Is E a function of parameter x and continuous t or something else?
You don’t provide enough information to answer your question in any detail.
Joshua Levin
on 21 Sep 2014
Answers (2)
Star Strider
on 21 Sep 2014
Thank you. The clarification makes all the difference.
If your x vectors are themselves functions of time, E does not necessarily have to be. Your output from ode45 would be a (Nx1) column vector t and an (Nx4) matrix x. Your function could then use element-wise operations (use .* instead of *, ./ instead of /, and .^ instead of ^) and reference x by column.
Example using your hypothetical E in an anonymous function:
t = [0:6]'; % Output From ‘ode45’
x = randi(10,7,4); % Output From ‘ode45’
E = @(x) 3.*x(:,1) + 2.*x(:,2) + cos(x(:,3)) - sin(x(:,4).^2);
Ev = E(x); % Evaluate E(x)
figure(1)
plot(t, Ev)
grid
So with the anonymous function construction, you don’t need the loop. The element-wise operations and referencing x by columns do it for you, and more efficiently. If you haven’t met Anonymous Functions yet, you will find them your new best friend!
4 Comments
Joshua Levin
on 21 Sep 2014
Edited: Joshua Levin
on 21 Sep 2014
Star Strider
on 21 Sep 2014
Edited: Star Strider
on 21 Sep 2014
My pleasure!
I need to know more about what you want M to be. The first element is obviously a scalar because all of its factors are scalars, as is the fourth element. (Note that it is ideally a (2x2) matrix.) The centre two are vectors because x(:,2) is a vector. (You cannot even compute M because of these problems.)
Also, it is going to be impossible to calculate E as I understand it. First x3 and x4 are column vectors, so qdot is going to be a (2*Nx1) vector as you have written it, not an (Nx2) matrix. That said, once you characterise it as (Nx2), the transpose of it would be (2xN), not conformable for multiplication by M, a (2x2) matrix. Once you get the first element of E calculated, note that the second element is going to be a column vector of length N, so the first element has to have the same dimensions.
I’ll be glad to help, but there are too many inconsistencies just now for me to work through it. You need to resolve them first.
Joshua Levin
on 21 Sep 2014
Star Strider
on 22 Sep 2014
The Symbolic Math Toolbox is not designed for what you want to do. (It’s primarily designed for derivations and one-off calculations.) It’s best you keep everything numeric. Besides, you have to resolve these problems if you are going to get numeric results and a plot in the end.
I’m not familiar with the equations you listed, so I can’t help you with insight into solving them. You likely need to explore your calculations in more detail. For instance, is M actually supposed to be a (2x2xN) matrix instead? This would make the calculation of E an iterative loop from [1:N] instead, not as neat but sometimes necessary.
Categories
Find more on Linear Algebra 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!