How to input discrete values describing state into an equation?
13 views (last 30 days)
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.
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!