number to variables in matrix

Dear all,
How can I apply below numbers to the specific matrix defined with respect to variable?
For example,
U=[x , 2*x];
And, we want to apply below numbers which are in matrix form:
X1=[0 1];
X2=[2 3];
Thanks

Answers (2)

I am not certain I understand what you want to do, so here are two possibilities, both using anonymous functions:
The first treats ‘x’ as a discrete single argument:
U1= @(x) [x , 2*x]; % Vector ‘x’
X1=[0 1];
X2=[2 3];
U1X1 = U1(X1)
U1X2 = U1(X2)
producing:
U1X1 =
0 1 0 2
U1X2 =
2 3 4 6
The second uses each element of ‘x’ separately:
U2 = @(x) [x(1) , 2*x(2)]; % Separate Elements of ‘x’
U2X1 = U2(X1)
U2X2 = U2(X2)
producing:
U2X1 =
0 2
U2X2 =
2 6

6 Comments

Thank you,
However, please note that the function (with respect to 'x') is not defined such that I wrote. I mean U is obtained by writing a long code and it is not same for different number of elements. For instance, it might be like this U=[x,x^2,x^3,x^4], and X1,X2,X3,X4 regarding the code is another variable.
thank you again!
My pleasure!
First, is ‘U1’ or ‘U2’ the format you intend? I need to know that before I proceed. I still don’t know if by ‘x’ you mean the entire vector for each ‘x’ in ‘U’ or the individual elements of ‘x’ for each element of ‘U’.
The ‘U’ function will likely be easy to write once we get this sorted.
TT
TT on 27 Sep 2014
Edited: TT on 27 Sep 2014
Here is the code:
ne=input('enter number of elements: ');
n=input('enter number of nodes: ');
L=input('enter the range: ')
le=L/ne
%Node point coordinates
e=1
X1(e:1:ne)=[0:le:L-le]
X2(e:1:ne)=[0+le:le:L]
%Shape functions
syms x
for i=1:ne
N1(i)=(X2(i)-x)/le;
N2(i)=(x-X1(i))/le;
end
N1
N2 % Interpolation displacement
d1=X1.^3
d2=X2.^3
% Define displacement in terms of shape functions
for i=1:ne
U(i)=N1(i)*d1(i)+N2(i)*d2(i);
end
U
for example, L=1, ne =2 ,n=3(should be 1+ne)! as you see we obtain 'U'. Now I want to continue the code by applying the first column of each X1,X2 to U(1) and second column of each X1,X2 to U(2).
And, generalize this to more number of elements!
thank you!
I am completely lost.
It seems to me that is exactly what you are doing, although indirectly, since ‘N1’, ‘d1’, ‘N2’ and ‘d2’ are all functions of the respective elements of ‘X1’ and ‘X2’.
Why?!
I obtain U as function of x but in matrix. Now, I just want to apply X1 and X2 's first column to the first column of U and X1 and X2 's second column on second column of U? I just could not write a code! it did not answer.
‘U’ is not a matrix. It is a row vector ‘ne’ elements long.

Sign in to comment.

Sorry, I can't interpret what you're after...are the Xn intended to become the factors in U, respectively? If that's so, then simply
U=[X1 2*X2];

Tags

No tags entered yet.

Asked:

TT
on 27 Sep 2014

Commented:

on 27 Sep 2014

Community Treasure Hunt

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

Start Hunting!