Creating multiples variables for syms - matlab

I want to create many variables such as x1, x2, x3 to use beside syms so it will look something like this:
syms x1 x2 x3 x4 ... x50 x51....xn
n is the number of variables I need.
Is there any way to do that?

x = sym('x', [n 1]);
This will create n symbolic variables i.e. x1, x2, x3 ......, xn and you can access them using x(1), x(2), x(3)....., x(n) respectively
For example with n=4, you'll get these results:
>> x
x =
x1
x2
x3
x4
>> x(1)
ans =
x1
>> x(3)
ans =
x3

Related

Particular vector operation without loops

Given a vector x1, x2, ..., xN, I need to create a vector of (x_i + x_j) for i = 1,...,N, j = i+1,...,N.
E.g., for x1, x2, x3, x4:
x1+x2, x1+x3, x1+x4, x2+x3, x2+x4, x3+x4
How to do it without loops to get good performance?
C = combnk(v,k) returns a matrix containing all possible combinations of the elements of vector v taken k at a time.
So if you call
combnk(x,2)
you get
x3 x4
x2 x4
x2 x3
x1 x4
x1 x3
x1 x2
In case you rely on the order, which is now inverted, use flipud, then call sum
sum(flipud(combnk(x,2)),2)

solve system of linear equations in matlab

I'm new to Matlab. Suppose I want to solve a linear system of 2 equations with 5 variables x1, x2, x3, x4, x5. Can Matlab give me solution for x1 and x2 in terms of the x3, x4, and x5? I also want to assign values to one or more variables, say I want to look at what happens if x3=5 or x3=3 and x5=1. Is there a way to achieve this?
I looked at the help page https://www.mathworks.com/help/symbolic/solve-a-system-of-linear-equations.html#d120e14359, but it does not cover the non-square matrix case
You can use multiple calls of solve to get solutions for x1 and x2. In this problem you can solve the first equation for x1, and then plug that into the second equation to get x2 in terms of x3, x4, and x5. You can then substitute the new value of x2 back into your solution of x1.
The subs function is used to substitute the solved values back into the original equation.
syms x1 x2 x3 x4 x5
eq1 = x1 + 4*x2 - 5*x3 + 2*x4 + x5;
eq2 = 3*x1 + 8*x2 - 3*x3 + x4 - x5;
x1s = solve(eq1, x1); % Solve x1 in term of x2-x5
x2s = solve(subs(eq2, x1, x1s), x2); % Solve x2 in terms of x3-x5
x1s = solve(subs(eq1, x2, x2s), x1); % Resolve x1 in terms of x3-x5
Output:
x1s =
3*x4 - 7*x3 + 3*x5
x2s =
3*x3 - (5*x4)/4 - x5
You can plug in values for x3, x4, and x5 using subs. For example, for x4=3 and x5=4:
subs(x1s, [x4 x5], [3 4])
ans =
21 - 7*x3

Matlab get symbols from matrix to assign value

I am creating a matrix of symbols and I declare a function using them:
x = syms('x', [1 2]);
f = x(1) + x(2)
So x and f are:
x = [x1 x2];
f = x1 + x2
Now I want to give values to x1 and x2 in a for loop and evaluate f. But when I use:
x(1) = value;
then x becomes:
x = [1 x2]
and x1 is lost, so I cannot evaluate f. How can I assign a value to x1, x2, ..., xn and then evaluate f?
You should use subs like the following:
subs(f,x1,value)
instead of replacing symbol of x1 with a value.
You can see the details of the function here.

Plotting a function with different ranges in Matlab

I would like to plot the following function:
I tried this code:
x = 0:0-1:4;
x1 = x(x<=-1);
x2 = x(x>-1 & x<=2);
x3 = x(x>2);
y1 = -3;
y2 = 1;
y3 = 4;
plot([x1 x2 x3], [y1 y2 y3])
But it is giving me the error that vectors must be the same length. How do I fix this error?
The problem is that matlab do not understand that y1=-3 means -3 for each value of x1. It needs a vector of -3s with as many elements as x1.
One way to define such a vector is the following:
x = -4:0.1:4;
x1 = x(x<=-1);
y1 = -3*ones(1,numel(x1));
So that you can plot the you want in the following way (look at it after giving it a try):
figure;hold on
x = -4:0.1:4;
x1 = x(x<=-1);
y1 = -3*ones(1,numel(x1));
plot(x1,y1)
x2 = x(x>-1 & x<=2);
y2 = ones(1,numel(x2));
plot(x2,y2)
x3 = x(x>2);
y3 = 4*ones(1,numel(x3));
plot(x3,y3)

Matlab gradient and hessian computation for symbolic vector function

I am trying to use the Matlab "gradient" and "hessian" functions to calculate the derivative of a symbolic vector function with respect to a vector. Below is an example using the sigmoid function 1/(1+e^(-a)) where a is a feature vector multiplied by weights. The versions below all return an error. I am new to Matlab and would very much appreciate any advice. The solution may well be under my nose in the documentation, but I have not been able to solve the issue. Thank you in advance for your help!
%version 1
syms y w x
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
gradient(f, w)
%version 2
syms y w1 w2 w3 x1 x2 x3 x w
x = [x1,x2,x3];
w = [w1,w2,w3];
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
%version 3
syms y w1 w2 w3 x1 x2 x3
f = (y-1)*[w1,w2,w3].*[x1,x2,x3] + log(1/(1+exp(-[w1,w2,w3].*[x1,x2,x3])));
Thanks, Daniel - it turns out the problem was in not having used dot() to take the dot product of w and x. Both diff() and gradient() gave the same solution, shown below:
syms y
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*dot(w,x) + log(1/(1+exp(dot(-w,x))));
diff(f, w(1))
gradient(f, w(1))
%ans =
%x1*(y - 1) + (x1*exp(- x1*conj(w1) - x2*conj(w2) - x3*conj(w3)))/
(exp(-x1*conj(w1) - x2*conj(w2) - x3*conj(w3)) + 1)