Passing the numeric matrix to symbolic function - matlab

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

Related

How to create an Octave function for evaluating sum of odd numbers in the vector only?

I created a function file on Octave like this,
function y = sum_odd(n)
if rem(n,2) ==1
sum_odd = sum(n)
endif
endfunction
and I want to make the function evaluate the sum of odd numbers in every vectors.
But when I tried sum_odd([1 2 3]), it didn't calculate for me.
Any ideas how to fix this problem?
You can simply use logical indexing:
function y = sum_odd(n)
y = sum(n(rem(n,2)==1))
end
rem([1 2 3],2)==1) returns logical array: [1 0 1].
n(logical([1 0 1])) returns[1 3]`.
sum(n(rem([1 2 3], 2) == 1)) returns sum of [1 3].

Dot product of symbolic vector

I am trying to take the dot product of a symbolic vector and another vector. I did the following:
>> rac = sym('rac',[3 1])
rac =
rac1
rac2
rac3
>> i = [1;0;0]
i =
1
0
0
>> dot(rac,i)
ans =
conj(rac1)
However my desired outcome is rac1. Why is it not behaving like I want it to? And how do I achieve this output?
You need to specify that your symbolic vector is real:
rac = sym('rac', [3 1], 'real');
dot(rac, [1; 0; 0])
ans =
rac1

Substitute symbolic variables with vectors

Consider the script
syms a b;
f = a.*b;
a = [1 2];
b = [0 2];
subs(f)
This yields a vector as output ([0, 4]), but the intended output is a scalar (4), given the element-wise multiplication that should be performed.
How can I properly utilize vectors when substituting symbolic functions?
I believe you're making two mistakes. The first is that you're overwriting your symbolic variables with double arrays, so you're not actually calling subs for a symbolic object:
>> syms a;
>> class(a)
ans =
sym
>> a = [1 2];
>> class(a)
ans =
double
The second is that preventing this will still give a wrong answer:
>> syms a b;
>> f = a.*b
f =
a*b
>> subs(f,{a,b},{[1, 2], [0,2]})
ans =
0 4
That's because, as you see in the printed version of f, the symbolic engine treats a and b as scalars.
So to do what you probably want to do you need to define your syms to be 2-element arrays:
> a = sym('a',[1 2])
a =
[ a1, a2]
>> b = sym('b',[1 2])
b =
[ b1, b2]
>> f = a.*b
f =
[ a1*b1, a2*b2]
>> subs(f,[a,b],[[1,2],[0,2]])
ans =
0 4
But anyway, as you can see, the result is still an array since .* (or symbolic *) is elementwise multiplication. In order to get a scalar product, use sym/dot:
>> dot(a,b)
ans =
b1*conj(a1) + b2*conj(a2)
>> subs(dot(a,b),[a,b],[[1,2],[0,2]])
ans =
4

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)

How to get a 3D-matrix or cell array efficiently by using vectorized code?

Here is what I want, a 3-D matrix:
K = 2:2.5:10;
den = zeros(1,4,4);
for i = 1:1:4
den(:,:,i) = [1, 5, K(i)-6, K(i)];
end
Or, a cell array is also acceptable:
K = 2:2.5:10;
for i = 1:1:4
den{i} = [1, 5, K(i)-6, K(i)];
end
But I want to know if there is a more efficient way of doing this using vectorized code like:
K = 2:2.5:10;
den = [1, 5, K-6, K];
I know the last code will not get what I wanted. But, like I can use:
v = [1 2 3];
v2 = v.^2;
instead of:
v = [1 2 3];
for i = 1:length(v)
v(i) = v(i)^2;
end
to get the matrix I want. Is there a similar way of doing this so that I can get the 3-D matrix or cell array I mentioned at the beginning more efficiently?
You need to "broadcast" the scalar values in columns so they are of the same length as your K vector. MATLAB does not do this broadcasting automatically, so you need to repeat the scalars and create vectors of the appropriate size. You can use repmat() for this.
K = 2:2.5:10;
%% // transpose K to a column vector:
K = transpose(K);
%% // helper function that calls repmat:
f = #(v) repmat(v, length(K), 1);
%% // your matrix:
den = [f(1) f(5) K-6 K];
This should be more optimized for speed but requires a bit more intermediary memory than the loop does.
Just use reshape with a 1*3 size:
den = reshape([ones(1,length(K));ones(1,length(K))*5; K-6; K],[1 4 length(K)]);
I think the used extra memory by reshape should be low and constant (dependent only on the length of the vector of new sizes).
You can use the classic line equation y=a*x+b, extended to the matrix form:
k = 2:2.5:10 ;
fa = [0 0 1 1].' ; %' // "a" coefficients
fb = [1 5 -6 0].' ; %' // "b" coefficients
d(1,:,:) = fa*k + fb*ones(1,4) ;
The above is better for clarity, but if you're not bothered you can also pack everything in one line:
d(1,:,:) = [0 0 1 1].' * (2:2.5:10) + [1 5 -6 0].' * ones(1,4) ;
If you need to re-use the principle for many different values of k, then you can use an anonymous function to help:
fden = #(k) [0 0 1 1].' * k + [1 5 -6 0].' * ones(1,4) ; %// define anonymous function
k = 2:2.5:10 ;
d(1,:,:) = fden(k) ; %// use it for any value of "k"