How to input discrete values describing state into an equation?

11 views (last 30 days)
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
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
Joshua Levin on 21 Sep 2014
x is a function of x1,x2,x3,x4, which are all continuous variables that are themselves functions of t. Using ode45, I solved dx/dt = f(x) and now I have discrete values of x1,x2,x3,x4 vs. t.
So yes, I have t - one column of values, and x - 4 columns of values which correspond to the time values.
Now I have a function E, let's say it's E = 3x1 + 2x2 + cos(x3) - sin(x4^2), and we know that x1,x2,x3,x4 are functions of time. So essentially, E is also a function of time, and I want to plot E vs. time (t). I could find E as a function of time at each individual point [t,x1,x2,x3,x4], so maybe I should make a loop to capture E at each t?
Sorry the question wasn't clear enough.

Sign in to comment.

Answers (2)

Star Strider
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
Joshua Levin on 21 Sep 2014
Essentially I want x1,x2,x3,x4 to be symbolic variables used in M and qdot so I can compute E as a function of those variables. Then when I have E, I can actually input the discrete values for x1,x2,x3,x4 (the vectors). Is this possible?
Star Strider
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.

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 21 Sep 2014
plot(t,x)

Categories

Find more on Programming 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!