How to input discrete values describing state into an equation?

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

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.
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)

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

Thank you! Here is my actual code which I am still having trouble implementing, although you have helped a lot!
x0 = [0; -15/(180*pi); 0; 0]; %initial conditions
tspan = [0 2]; %time span
[t,x] = ode45('myfunction', tspan, x0); %ode. Note: 'myfunction' defined elsewhere
%constants
mp = 0.127;
Lp = 0.337;
mr = 0.257;
Lr = 0.216;
g = 9.81;
tao = 0;
x1 = x(:,1);
x2 = x(:,2);
x3 = x(:,3);
x4 = x(:,4);
M = [mp*Lr^2+mr*Lr^2/3, -cos(x2)*Lr*Lp*mp/2; -cos(x2)*Lr*Lp*mp/2, mp*Lp^2/3];
fnon = [sin(x2)*Lr*Lp*mp*power(x4,2)/2; -sin(x2)*g*mp/2];
b = [1;0];
qdot = [x3; x4];
E = [1/2*transpose(qdot)*M*qdot + cos(x2)*g*mp*Lp/2];
I am still running into trouble because if I define x1,x2,x3,x4 as I did, the matrices don't compute properly. For example, M = [a,b;c,d] now has one value for a and d but b and c have N values....I also cannot invert M if it is not square
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.
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?
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.

Asked:

on 21 Sep 2014

Commented:

on 22 Sep 2014

Community Treasure Hunt

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

Start Hunting!