Plug in matrix values into a Formula

7 views (last 30 days)
Hi, can some one help me with the following:
I have matrix U and X
U=[-1.5 -1.0 -.5 0]; X = [1.5 0 0 0 0];
I want to substitute all the values of Big X and Big U into small x and u in following equation:
syms x u; X_E=x^2+*u
and i want my results to be in a matrix form. I was doing the following; however i want to find out a more efficient way to do it
X_Next=[subs(X_E,[x u],[X(1,1) U(1,1)]); subs(X_E,[x u],[X(1,1) U(1,2)]) ;subs(X_E,[x u],[X(1,1) U(1,3)]) ;subs(X_E,[x u],[X(1,1) U(1,4)])]
Thanks a lot in advance.
  3 Comments
Fredy
Fredy on 22 May 2014
Edited: Fredy on 22 May 2014
How about if lets say my X matrix is [1 2 3 4 5 6] and U is [1 2] and i want to calculate
F(x,u)=F(1,1)
=F(1,2)
=F(2,1)
=F(2,2)
=F(3,1)
=F(3,2) etc.
and display it as a Vector(x rows and u columns)

Sign in to comment.

Accepted Answer

dpb
dpb on 22 May 2014
Edited: dpb on 22 May 2014
>> x=1:6;u=1:2;
>> [X,U]=meshgrid(x,u); % make commensurate sizes...
>> f(X(:),U(:)) % evaluate vectors
ans =
2
3
5
6
10
11
17
18
26
27
37
38
>>
  5 Comments
dpb
dpb on 22 May 2014
Edited: dpb on 22 May 2014
We defined the function handle f earlier, remember????
>> f=@(X,U) X.^2+U; % the functional form
f =
@(X,U)X.^2+U
Now we've just evaluated it with a different set of inputs.
Try the above without the (:) on the two arrays to make a vector and see what happens. Also look in detail at the output of meshgrid call and check the doc's on how that works.
Fredy
Fredy on 26 May 2014
Edited: Fredy on 26 May 2014
(Note: i attached the question in a .txt document since the format here does not look very organize)
Could you please look at the attach document. What you see in the tables is what I am trying to do with matlab. So far with your help I am able to come up with all columns except the last two since i do not know how to get the minima of a matrix in a 5 row interval. Do you think i am using the right approach? Here is my code so far.
u=[1 0.5 0 -0.5 -1]; %Given u x = [1.5 1 0.5]; %Given x X_K_1=@(X,U)X+U; %Next State of X(K+1) [X,U]=meshgrid(x,u); % X_LAST=[X_K_1(X(:),U(:))]; % Plugs in all values of x and u into X_K_1
F_1=input('Please enter a value for X: ') % User input is X_LAST.^2 U_1=input('Please enter a value for U: ') %User input is 2.*U.^2
J_2_1=@(X_LAST,U_1)F_1+U_1; %Formula for J_2_1 in terms of F_1 and U_1 function
J_COST=[J_2_1(X_LAST(:),U(:))] % Cost for all values of x and u in formula J_2_1
IF you run this, i Get the following Result: J_COST =
8.2500
4.5000
2.2500
1.5000
2.2500
6.0000
2.7500
1.0000
0.7500
2.0000
4.2500
1.5000
0.2500
0.5000
2.2500
Now, here where I am a bit lost on how i go about selecting the minima of every 5 rows. For example the first minima is from : [ 8.2500 4.5000 2.2500 1.5000 2.2500] which is 1.5.
The next minima will be from row 6 to 10. Etc...
Thank you so much for all your help!

Sign in to comment.

More Answers (3)

dpb
dpb on 27 May 2014
Edited: dpb on 27 May 2014
...how i go about selecting the minima of every 5 rows...
OK, here is where you need to think (and it helps when you get to where you dream in :) ) Matlab--
>> min(reshape(J_COST,5,[])).'
ans =
1.5000
0.7500
0.2500
>>
I'll let you think on that a while... :) HINT: "memory storage order"
  7 Comments
dpb
dpb on 27 May 2014
...reshaping J_COST by a new matrix that has J_COST minimun in every 5 rows,
Close, but not, precisely, no...read from the inside out and if needed look at the result of the intermediary steps to see what's happening. As mentioned before, the "how" is significant piece of understanding that will prove to be invaluable going forward in using Matlab. As said before, it's tied into internal storage order in Matlab which is "column major".
Fredy
Fredy on 27 May 2014
Thx a lot again dpb, I was able to construct my first table, now i would like to know if its possible to create a Matrix which Looks at matrix Z and scans my State column and once it finds the value equal(or nearest) to my State column of,it will select its corresponding OptimalJ. Also if Z is greater than any value of my state column it will as
For example: for my first value of Z which is 2.5, since its out of range from my state column i want to put a large number maybe 10000 or a blank space that still exist in my matrix. The fourth value of Z is 1.0, so i would like to select 0.75 from my OptimalJ.
T =
State OptimalJ OptimalU
_____ ________ ________
1.5 1.5 -0.5
1 0.75 -0.5
0.5 0.25 0
>> Z =
2.5000
2.0000
1.5000
1.0000
0.5000
2.0000
1.5000
1.0000
0.5000
0
1.5000
1.0000
0.5000
0
-0.5000
so the result will look something like this =
x------------------>blank or large number 1000(
x-----------------> since i will be finding minimuns again.
1.5000
0.7500
0.2500 ....etc
I was playing with find function but i had no success, thanks a lot again!!

Sign in to comment.


dpb
dpb on 27 May 2014
interp1(T(:,1),T(:,2),Z,'nearest',NaN)
  20 Comments
dpb
dpb on 1 Jun 2014
One more time...example data, results...
Fredy
Fredy on 3 Jun 2014
dpb, i would like to thank you once again! almost a week ago i knew nothing about matlab, with your help i successfully completed my code, and it worked perfectly!
THANK YOU AGAIN!

Sign in to comment.


Natalya Shedlovskiy
Natalya Shedlovskiy on 10 May 2022
how I can plug Major heed losses formula

Community Treasure Hunt

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

Start Hunting!