solve system of linear equations in matlab - 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

Related

Get differential equation of derivative

I have a differential equation that looks like this:
dT/dx = (1+alpha\*M1)\*(T^2)\*S(x) - 4\*(2+gamma\*M2)\*x\*T
I want to differentiate this equation let's say over alpha. However, T depends on alpha. How can I get a differential equation that includes the derivative dT/da so I can solve for it (using ode45 or something)?
Note: I can't differentiate the equation by hand as I will need to do this for the Ys as well, where I don't have an easy analytical function.
My code so far:
syms L alpha gamma Y2 Y3 Y4 Y5 Y6 x T X
M1 = 0;
M2 = 7;
Y1 = 1 + M2 / 10;
Y7 = 3 + M1 / 10;
xcp = [0 0.1*L 0.25*L 0.5*L 0.6*L 0.75*L L];
ycp = [Y1 Y2 Y3 Y4 Y5 Y6 Y7];
T0 = 5 + 1/L - 25/(L^2);
S = bezier_syms(); % Assume this returns a function of L,x,Y2,Y3,Y4,Y5,Y6
ode = diff(T,x) == (1+alphaM1)*(T^2)*S(L,x,Y2,Y3,Y4,Y5,Y6) - 4*(2 + gamma*M2)xT;
Given the equation
dT/dx = F(x,T,a)
and U=dT/da, then by the rules of differentiation like the chain rule, you get
dU/dx = dF/da(x,T,a) + dF/dT(x,T,a)*U
where the partial derivatives are easy to determine. For more precise formulas, see https://math.stackexchange.com/a/3699281/115115

Solve equation set using MATLAB syms

The question image
I have write the MATLAB code, like this
syms x x1 x2 x3 y y1 y2 y3 t0 t1 t2 t3 v
[x,y]=solve((x-x1)^2+(y-y1)^2-(v*(t1-t0))^2==0,(x-x2)^2+(y-y2)^2-(v*(t2-t0))^2==0,(x-x3)^2+(y-y3)^2-(v*(t3-t0))^2==0)
but the ans is empty, like this
x =
Empty sym: 0-by-1
y =
Empty sym: 0-by-1
In fact, the equation set does have a solution, so I want to know how to correct the code?
The docs say
If solve returns an empty object, then no solutions exist.
Cleaning up your problem a bit,
Eq1 = (x-x1)^2+(y-y1)^2-(v*(t1-t0))^2==0;
Eq2 = (x-x2)^2+(y-y2)^2-(v*(t2-t0))^2==0;
Eq3 = (x-x3)^2+(y-y3)^2-(v*(t3-t0))^2==0;
EqSys = [Eq1,Eq2,Eq3]; % create a system of equations
solve(EqSys,x) % <= specify, for which variable the system of equations should be solved
made me feel that you want to solve vectors rather than distinct variables, right? Are the entries independent? Or is the solution x indeed a scalar value?
If not, solving a single equation
solve(Eq1,x)
does return an answer
ans =
x1 + (y - y1 + t0*v - t1*v)^(1/2)*(y1 - y + t0*v - t1*v)^(1/2)
x1 - (y - y1 + t0*v - t1*v)^(1/2)*(y1 - y + t0*v - t1*v)^(1/2)

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)

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.

Creating multiples variables for syms

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