How to find the linear parameterization of a symbolic vector in MATLAB - matlab

I have a symbolic vector y which I know contains linear expressions of the variables contained in a symbolic vector theta. Is there a way to compute the symbolic expression of A, where y = A * theta? I tried y*pinv(theta) but it doesn't seem to work.
Example:
syms a b real
theta = [a;b];
y = [2*a;2*b];
y*pinv(theta) gives
ans = [ (2*a^2)/(a^2 + b^2), (2*a*b)/(a^2 + b^2)]
[ (2*a*b)/(a^2 + b^2), (2*b^2)/(a^2 + b^2)]
and
y/theta gives
ans = [ 2, 0]
[ (2*b)/a, 0]
along with a warning that the solution is not unique.
I want to eradicate the symbolic variables from my result i.e. I want
ans = [ 2, 0]
[ 0, 2]

The equationsToMatrix function seems to be doing the job!
Try equationsToMatrix(y,theta)

Related

Passing the numeric matrix to symbolic function

I have created a function containing symbolic expressions. The expressions make use of a symbolic matrix. I want to solve the function passing the numeric matrix which replaces the symbolic matrix and provides a numeric answer. I can't seem to understand how to pass the numeric matrix.
function example
R= sym('R',[3 3])
r_b= R([1 2], [3])
R_bar= R([1 2], [1 2])
R_til=R([2 3],[2 3])
syms alpha_bar beta_bar
a_bar= [1;alpha_bar]
b_bar= [beta_bar;1]
P= sym(R_bar*a_bar)
Q= sym(R_til*b_bar)
syms E_a E_b
u_bar= [1; 0]
v_bar= [0;1]
W = sym(E_a*u_bar)
X= sym(E_b*v_bar)
C= sym(P==W)
D= sym(Q==X)
[alpha_bar_, E_a_] = solve(P==W,[alpha_bar,E_a])
[beta_bar_, E_b_] = solve(Q==X,[beta_bar,E_b])
a_bar= [1;alpha_bar_]
b_bar=[beta_bar_;1]
delta= (a_bar)'*r_b
gamma_b = delta/E_b_
gamma_a= delta/E_a_
a= [a_bar;0]-(gamma_b*[0;b_bar])
b= [0;b_bar]-gamma_a*[a_bar;0]
end
My R is R = [1 1 0; 1 3 2; 0 2 3].

MATLAB - subs method doesn't work on symbolic vector of indexed variables

Consider the following code:
A = sym('a', [1, 2]);
b = sym('b');
ans = A.^2 + b;
A = [1, 2];
b = 4;
subs(ans)
This yields the output
ans = [ a1^2 + 4, a2^2 + 4]
Whereas I would have wanted it to produce
ans = [ 5, 8]
What is required for the vector to be converted to numeric values aswell?
Here's a simpler solution:
syms A b; %Initializing symbolic variables
expr = A^2 + b; %Your expression (element-wise square is taken by default for sym class)
A = [1 2]; b=4; %Assigning the values
subs(expr) %Substituting symbolic variables with corresponding assigned values
Output as desired:
ans =
[ 5, 8]
Comments:
Always avoid dynamic variables. In your code, you're specifying A to be [1, 2] but your expression doesn't actually have A in it. It has a1 and a2 (and b obviously).
Don't name your variables/functions after the reserved variable names or in-built functions. ans is used for the Most Recent Answer when expressions are not assigned to anything else. (That's why I replaced ans in your code with expr)

Passing a Vector to Another Vector?

What I have:
syms X Y
funcF = [ (X.^2 + Y.^2 + X -4), (Y*cos(X) + X*Y.^3 - 1) ]
z0 = [0.5, 2]
Say I want to pass z0 to funcF, I'm not sure how to do this. (My knowledge of matlab isn't the greatest)
So I'm currently trying:
funcF = funcF(z0)
Essentially, I want it to print something like this:
[#1 #2]
Because that expression is symbolic, you need to use subs. subs replaces symbolic variables with whatever you want... and that includes numbers. Also, because this is a symbolic expression (thanks Nasser), there is no need for point-by-point operators (i.e. .^). You can remove these and use the normal scalar operators.
As such, you want to replace X and Y in the expression with the corresponding values in z0:
>> syms X Y;
>> funcF = [ (X^2 + Y^2 + X -4), (Y*cos(X) + X*Y^3 - 1) ];
>> out = subs(funcF, {'X','Y'}, z0)
out =
[ 3/4, 2*cos(1/2) + 3]

How to evaluate a Jacobian 3x3 matrix in Matlab?

I need to compute Jacobian for a nonlinear problem, f(x)=0, where f(x) is a system of these nonlinear equations:
x^5 + y^3*z^4 + 1;
x^2*y*z;
z^4-1;
I need to compute the Jacobian J(x) for this system of nonlinear equations in Matlab2009. After using these comands:
syms x y z
jacobian([x^5 + y^3*z^4 + 1 ; x^2*y*z ; z^4-1 ;],[x; y; z])
I get this:
ans =
[ 5*x^4, 3*y^2*z^4, 4*y^3*z^3]
[ 2*xyz, x^2*z, x^2*y]
[ 0, 0, 4*z^3]
How can I evaluate this? The note on this example is (Note that the Jacobian is singular for z=0)... How can I do this?
Well, you could either write the code for the jacobian yourself:
J = #(x,y,z) [ 5*x.^4, 3*y.^2.*z.^4, 4*y.^3*z.^3; ...
2*x.*y.*z, x.^2.*z, x.^2.*y; ...
0, 0, 4*z.^3];
Or directly generate a function handle from the symbolic expression using matlabFunction:
syms x y z;
J = matlabFunction(jacobian([x^5 + y^3*z^4 + 1 ; x^2*y*z ; z^4-1 ;],[x; y; z]));
You can then evaluate it via J(1,2,3).

equationsToMatrix: how do I get the values of the variables?

When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)
syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])
%solution of the equation set
A =
[ 1, 1, -2]
[ 1, 1, 1]
[ 0, 2, -1]
b =
0
1
-5
The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.
The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z.
What I would need is to get something like this in the end
b(1) = 0
or
x = 0
What should I do to get the values of x,y,z by just typing x,y,z?
What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do
sol = A\b;
and now you can use the values in another expression with
subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));
for example
A =
1 1 -2
1 1 1
0 2 -1
b =
0
1
-5
>> A\b
ans =
3.0000
-2.3333
0.3333