Using symbolic variable in boolean algebra with MATLAB - matlab

How do I substitute the symbolic variables in a boolean expression? I am having this problem and can't solve it even in the example below.
syms a
syms b
o = myand(a, b);
where
function o = myand(a, b)
o = and(a, b);
end
I also tried the below but got an "Invalid Operand" error.
subs(o, [a, b], [1, 1])
I also tried using the not() function (one input) and it worked. I read MATLAB documentation and I cannot figure out what I am doing wrong.

Ah, so for the and expression from the Symbolic Toolbox in MATLAB, it requires an actual mathematical expression for each operand. The reason why the not operator worked for you is because it is able to take in numbers, vectors, anything that is numeric in addition to symbolic expressions. From the not documentation:
A — Input
number | vector | matrix | array | symbolic number | symbolic variable | symbolic array | symbolic function | symbolic expression
Compared with and:
A - Input
symbolic equations | symbolic inequalities | symbolic expressions | symbolic arrays
So the and function actually does not take in single values when you do substitutions. Also, I suspect that if you just use a and b as is, it doesn't actually understand that if a is not zero, this should be true. The same goes for b. A workaround is to define the function, but condition both variables so that anything greater than 0 is true, else false. This should still hold within the binary universe:
function o = myand(a, b)
o = and(a > 0, b > 0);
end
For this to be more compact, we can define an anonymous function instead:
myand = #(a, b) and(a > 0, b > 0);
Now if you try it, it should work:
syms a b;
myand = #(a, b) and(a > 0, b > 0);
o = myand(a, b);
c = subs(o, [a, b], [1, 1]);
We get for c:
>> c
c =
0 < 1
... so yeah, that's technically true. We can convert this into a numeric result:
>> double(c)
ans =
1

Related

Identify powers in an algebraic expression for a Buckingham Pi calculation in MatLab

This is a continuation of an earlier question I asked here.
If I create a symbolic expression in MatLab
syms L M T
F = M*L/T^2
I want to identify the powers of each dimension M, L, or T. In this case, the answer should be
for M, 1
for L, 1
for T, -2
There is a relatively easy way to do this if the expression F were a polynomial in MatLab employing the coeffs function. However, my expression is clearly not a polynomial as far as MatLab is concerned.
In the end, I will be working with at least two parameters so I will put them in a cell array since I anticipate cellfun will be useful.
V = L/T
param = {F,V};
The final output should be a table where the rows correspond to each dimension, L M and T and the columns are for each parameter F and V.
syms L M T
F = M*L/T^2
[C,T] = coeffs(expand(log(F),'IgnoreAnalyticConstraints',true))
[exp(T).' C.']
It returns the table:

Why does MATLAB fail to check the equality of this trigonometric expression

isequaln() is testing symbolic objects for equality as stated in the documentation. However, this is not the case with the following script.
syms a
f1=cos(a)^2;
f2=1-sin(a)^2;
isequaln(f1,f2)
ans =
logical
0
MATLAB does not return the correct answer. What does MATLAB do when comparing equality for symbolic expressions, compare strings (i.e. a typical scenario for regular expressions), or something else?
At the bottom of the documentation page, there is a section called "Tips", which contains the following item:
isequaln(A,B) checks if A and B are the same size and their contents are syntactically the same expression, treating NaN values as equal. To check whether the mathematical comparison A == B holds for all values of variables in A and B, use isAlways(A == B).
(emphasis mine)
isAlways does what you want:
syms a
f1 = cos(a)^2;
f2 = 1-sin(a)^2;
isAlways(f1 == f2)
This outputs true.
Alternatives:
>> simplify(f1-f2)
ans =
0
>> simplify(f1==f2)
ans =
symtrue

Octave/Matlab: Difference between e^(-1*z) and exp(-1*z)

I am new with Octave and I have a problem.I thought the following codes were the same, but they produces different results. What is the difference? Thanks
Octave/Matlab: Difference between e^(-1*z) and exp(-1*z)
g = 1./(1 + e^(-1*z));
g = 1./(1 + exp(-1*z));
Where z is a vector, element or matrix
In Octave
exp(1) equals e where e is Euler's number.
There are 4 operations/functions that are to be noted here:
e^x is same as expm(x) and e.^(x) is same as exp(x).
e^x and expm(m) represent e raise to the matrix x.
e.^(x) and exp(x) represent exponential ex for each element in matrix x.
If x is a scalar then all (e^x, expm(x), e.^x and exp(x)) are mathematically equal.
For your case, z is a matrix and hence you get different results.
In MATLAB,
e is not defined in MATLAB. exp(x) and expm(x) have same definitions in MATLAB as those that are described for Octave above.
PS: e or E are also used for E-notation in both MATLAB and Octave but that's a different thing.
In Octave, it is important to note that e^x and exp(x), where x is a double precision scalar variable, are not necessarily the same.
For instance:
>> a = e ^ 2
a = 7.3891
>> b = exp (2)
b = 7.3891
>> b - a
ans = 8.8818e-16
The reason is that exp (2) uses a dedicated algorithm to compute the exponential function, while e ^ 2 actually calls the function e () to get the value of e, and then squares it:
>> c = realpow (e (), 2)
c = 7.3891
>> c - a
ans = 0
Another reason why e ^ x and exp (x) differ is that they compute completely different things when x is a square matrix, but this has already been discussed in Sardar's answer.

Summation of sines with limits and a variable?

I need help to add a function, from 1 to 10, using MATLAB. The function is ((1/n)*sin(n*pi*x)) where n goes from 1 to 10 and x stays as a variable. Ultimately I want to have a summation of ten sines (i.e K1*sin(pi*x)+K2*sin(2*pi*x)+k3*sin(3*pi*x)+...etc) where k is a constant. I would really appreciate any assistance. Thanks
Edit: Thanks to everyone who helped with my problem However I should have been more specific when asking my question. After getting the summation, I want to plot the sine series. I tried doing this but I kept getting an error saying that "conversion from sym to double is not possible" Now I tried doing a for loop to get my graph. My code is as follows:
n = 0:10;
while i <= n
for i = 1:length(n);
T = (1/n(i))*sin(n(i)*pi*x);
end
i = 1+i;
max = sum(T);
end
plot(x,max,'black')
However this doesn't work. I don't think that this is the proper way to get the sum of a double. I would really appreciate it if someone could help me again. Thanks again
Learn to exploit MATLAB's vector nature.
For one-off shots:
>> f = #(n,x) sin((1:n)*pi*x) * (1./(1:n).');
>> f(200, 0.5)
ans =
7.828982258896381e-001
To be able to evaluate f(n,x) with vector/matrix input x:
>> f = #(n,x) reshape( sin( bsxfun(#times, (1:n)*pi, x(:)) ) * (1./(1:n).'), size(x) );
>> f(15,rand(2))
ans =
5.077194963950054e-001 2.660834723822258e-001
1.416130930552744e+000 1.012255979042172e-001
Replace (1./(1:n).') by [K1 K2 K3 ...].' when you want to use other constants than 1/n.
From my understanding you are trying to sum the multivariable expression. In your case, you have two variables n and x. You want to sum the expression from n = 1 to 10 keeping x as a variable. You can do this in MATLAB by using symsum function, whose syntax is
symsum(expr,var,a,b)
Where expression expr defines the terms of a series, with respect to the symbolic variable var. The value of the variable var changes from a to b. If you do not specify the variable, symsum uses the default variable determined by symvar. If expr is a constant, then the default variable is x.
So in your case
expr = (1/n*sin(n*pi*x)
var = n
a = 1
b = 10
The simple code would be
>>syms n x
>>F = symsum ((1/sym('n'))*sin(sym('n')*pi*x), n, 1, 10)
Answer to Edit: MATLAB cannot convert the sys variable to double. You can instead substitute the value of sym variable with variable from MATLAB workspace. For example you can plot the above function for the range 0 to 10 by using following command.
>> x = 0:0.1:10;
>> plot(x, subs(F))

Apply functions to all elements of an array of matrices

I have four 52-by-140 matrices in Matlab. Lets call them a, b, c, and d:
I want to apply the eigs function to a Hessian matrix [a,b;c,d] for every point in the original matrix like:
for i = 1:52
for j = 1:140
eigs([a(i,j),b(i,j);c(i,j),d(i,j)])
end
end
How can I do this in a simpler way, i.e., without for loops?
This can probably be done using arrayfun and defining the action you do in a single iteration using an anonymous function (untested)
result = arrayfun(#(a_ij, b_ij, c_ij, d_ij) eigs([a_ij, b_ij; c_ij, d_ij]), ...
a, b, c, d, 'uniformoutput', false);
Note that since eig returns a vector, the result can only be stored in a cell array.
You could name the parameters of the anonymous functions a, b, ... instead of a_ij, b_ij, ..., since they are only used inside the function, but I prefer to it this way to make it clear that inside the function you are using scalars, while the parameters to arrayfun are matrices. Personally, I often use upper/lower case to indicate the difference:
result = arrayfun(#(a, b, c, d) eigs([a, b; c, d]), A, B, C, D, 'uni', 0);
but then you would have to rename your variables.
Try this solution to get all of the matrixes teed up:
>> abcd = cat(3,a,b,c,d);
>> H = permute(reshape(permute(abcd,[3 1 2]),2,2,[]),[2 1 3]);
>> size(H)
ans =
2 2 7280
>> i=3;j=2;
>> [a(i,j),b(i,j);c(i,j),d(i,j)]
ans =
0.4984 0.7935
0.3524 0.2273
>> H(:,:,i+(j-1)*size(abcd,1))
ans =
0.4984 0.7935
0.3524 0.2273
>>
Then to run eigs on all 2D matrixes in H:
E=arrayfun(#(i)eigs(H(:,:,i)),1:size(H,3),'uni',false);