solve complex equations in MATLAB (Variable Equations) - matlab

I have some equations which include some parameters and variables.
Each variable defines and also exists in other equations. these are my equations:
syms Pof s Pon teta landa PIon PIof PISC
Pof = (s * y - teta + T * teta - landa + Pi * landa + alpha * p + Pon * b1 + w * b2)/(2 * b2);
s = -(y * (w - Pof))/q;
Pon = (q * s * y + 2 * w * y - 2 * w * y^2 + 2 * q * alpha + 2 * q * Pi * landa - q * teta + 3 * q * T * teta - q * landa + q * Pi * landa - q * alpha * p - 2 * y * Pof + 2 * y^2 * Pof + C * q * b1 + q * w * b2)/(2 * q * b1);
teta = (C - 3 * C * T - Pon + 3 * T * Pon)/2 * q;
landa = (-1/2) * (-1 + Pi) * (C - Pon);
Don = (1-p) * alpha - b1 * Pon + b2 * Pof - (1-y) * s + T * teta + landa * Pi;
Dof = p * alpha - b1 * Pof + b2 * Pon + y * s - (1-T) * teta - landa * (1-Pi);
PIon = (Pon-C) * Don - (1/2) * n * teta^2 - (1/2) * q * landa^2;
PIof = (Pof-w) * Dof - (1/2) * L * s^2;
PISC = PIon + PIof;
How I can solve these in order to get a numeric answer for each variable?
(I don't want parametric answers)

The equations as stated are
which can be arranged as a linear system A x = b as follows
which you solve in Matlab as x = A \ b
On further investigation, it seems A is singular since it's 10×8 in size and cannot be inverted. So a least-squares solution is needed where
x = inv(AT* A)* AT b

Related

"fsolve" doesn't work for system nonlinear equation

I have these equations:
syms pm pr teta s
A1 = -2 * b1 * pm + 2 * b2 * pr + b * teta + (1-t) * s + (1-p) * a + c * (b1 - b2);
A2 = 2 * b2 * pm + 2 * b1 * pr + (1-b) * teta + t * s + p * a + c * (b1 - b2);
A3 = b * pm + (1-b) * pr - n * teta - c;
A4 = (1-t) * pm + t * pr - k * s - c;
eqns = [A1,A2,A3,A4];
F=#(pm, pr, teta, s) [A1
A2
A3
A4];
x0 = [10, 10, 10, 10];
fsolve(F, x0)
How I can solve them?
(When I use fsolve, it shows this error: FSOLVE requires all values returned by functions to be of data type double)
Since you tagged Mathematica
A1 = -2*b1*pm + 2*b2*pr + b*teta + (1 - t)*s + (1 - p)*a + c*(b1 - b2);
A2 = 2*b2*pm + 2*b1*pr + (1 - b)*teta + t*s + p*a + c*(b1 - b2);
A3 = b*pm + (1 - b)*pr - n*teta - c;
A4 = (1 - t)*pm + t*pr - k*s - c;
FullSimplify[Solve[{A1 == 10, A2 == 10, A3 == 10, A4 == 10}, {pm, pr, teta, s}]]
pm -> ((k ((-1 + b)^2 + 2 b1 n) + n t^2) (b (10 + c) k +
n (10 + c + 10 k - a k - b1 c k + b2 c k +
a k p - (10 + c) t)) + ((-1 + b) (10 + c) k +
k n (-10 + b1 c - b2 c + a p) - (10 + c) n t) (b k - b^2 k +
n (2 b2 k + t - t^2)))/(k n (1 - 2 b1 k + b^2 (1 + 4 b2 k) +
2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t)))
pr -> (c +
b^2 (10 + c - (-20 + a + 2 b1 c - 2 b2 c) k) + 2 b1^2 c k n -
b2 (20 + c - 2 (-10 + a) k + 2 b2 c k) n - a (1 + 2 b2 k) n p -
2 b1 k (10 + c + n (10 - a p)) + 10 (1 + n - 2 t) -
c (2 + b2 n) t +
n (-30 + a + 20 b2 + a p) t + (10 + c - (-20 + a) n +
2 b2 c n) t^2 - b1 n (c + 20 t + c t (-1 + 2 t)) +
b (k (-10 + a + 20 b1 - 20 b2 - a p) + 20 (-1 + t) +
c (-2 + 3 b1 k - 3 b2 k + 2 t)))/(1 - 2 b1 k +
b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
teta -> (10 - 20 b2 - b2 c -
20 b2 k + 2 a b2 k + 40 b2^2 k + 2 b2^2 c k +
2 b1^2 (20 + 3 c) k - a p -
2 a b2 k p + (-30 + 3 b2 (20 + c) + a (1 + p)) t - (-20 + a +
2 b2 (20 + c)) t^2 +
b (-10 - 4 b1^2 c k + a p +
b1 (20 + 40 k - 2 a k + c (3 + 4 b2 k - 2 t)) + 20 t - a t +
b2 (20 + c - 2 a k + 4 a k p - 2 (20 + c) t)) +
b1 (2 k (-10 + a p) + 20 (-1 + t) + c (-3 + (5 - 2 t) t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
s -> (20 b1 + b1 c - b2 c -
2 b^2 (-10 + b1 c + b2 (20 + c)) + 20 b1 n + 40 b1^2 n -
20 b2 n + 40 b2^2 n + 2 b1^2 c n + 4 b1 b2 c n +
2 b2^2 c n - (b1 - b2) (20 + c) t +
4 b1 (-10 + b1 c - b2 c) n t - 10 (-1 + 2 b2 + t) +
a (-1 - b^2 - 2 b1 n + p + 2 (b1 + b2) n p + t - p t +
2 n (b1 + b2 - 2 b2 p) t - b (-2 + p + t)) +
b (-(-10 + b2 (20 + c)) (-3 + 2 t) + b1 (-20 + c - 2 c t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))

How to create a matrix in Matlab with every entry being the output of a bivariate function

I want to create a 4 x 4 matrix with each entry representing f(x,y) where both x and y take values 0, 1, 2 and 3. So the first entry would be f(0,0), all the way to f(3,3).
The function f(x,y) is:
3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y) + 8 * cos(0*x + 3*y)
+ 3 * cos(1*x + 0*y) + 25 * cos(1*x + 1*y) + 3 * cos(1*x + 2*y)
+ 8 * cos(1*x + 3*y)
+ 3 * cos(2*x + 0*y) + 25 * cos(2*x + 1*y) + 3 * cos(2*x + 2*y)
+ 8 * cos(2*x + 3*y)
+ 3 * cos(3*x + 0*y) + 25 * cos(3*x + 1*y) + 3 * cos(3*x + 2*y)
- 90 * cos(3*x + 3*y)
I haven't used Matlab much, and it's been a while. I have tried turning f(x,y) into a #f(x,y) function; using the .* operator; meshing x and y, etc. All of it without success...
Not sure, what you've tried exactly, but using meshgrid is the correct idea.
% Function defintion (abbreviated)
f = #(x, y) 3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y)
% Set up x and y values.
x = 0:3
y = 0:3
% Generate grid.
[X, Y] = meshgrid(x, y);
% Rseult matrix.
res = f(X, Y)
Generated output:
f =
#(x, y) 3 * cos (0 * x + 0 * y) + 2 * cos (0 * x + 1 * y) + 3 * cos (0 * x + 2 * y)
x =
0 1 2 3
y =
0 1 2 3
res =
8.00000 8.00000 8.00000 8.00000
2.83216 2.83216 2.83216 2.83216
0.20678 0.20678 0.20678 0.20678
3.90053 3.90053 3.90053 3.90053

How to use "field" with rationals?

Can anybody tell me why the tactic "field" does not work when I try to prove the following goal involving rationals?
nat_to_Q 3 * nat_to_Q n * nat_to_Q n + nat_to_Q 3 * nat_to_Q n +
nat_to_Q 3 * nat_to_Q n + nat_to_Q 3 + nat_to_Q 3 * nat_to_Q n +
nat_to_Q 3 + nat_to_Q n * nat_to_Q n * nat_to_Q n + nat_to_Q n * nat_to_Q n +
nat_to_Q n * nat_to_Q n * nat_to_Q 2 + nat_to_Q n * nat_to_Q 2 ==
nat_to_Q 3 * nat_to_Q n * nat_to_Q n * nat_to_Q n +
nat_to_Q 6 * nat_to_Q n * nat_to_Q n + nat_to_Q 11 * nat_to_Q n +
nat_to_Q 6
Note: n is nat and nat_to_Q is (Z.of_nat n # Pos.of_nat 1).
Thanks a lot,
Marcus.
Let's remove the coercions to make this easier to read:
3 * n * n + 3 * n +
3 * n + 3 * n +
3 + n * n * n + n * n +
n * n * 2 + n * 2 ==
3 * n * n * n +
6 * n * n + 11 * n + 6
Now we can see the issue: the goal does not hold. The 3rd order coefficient on the left-hand side is 1, but on the right-hand side it is 3.

how to solve simultaneous equations with solve and with result without roots

Here is the code:
syms G1 G2 G3 M1 M2 M3 P1 P2 P3 D1 D2 D3
S = solve(0 == 0.9 * (20 - G1) - 0.012 * D3 * G1, ...
0 == 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.0033 * M1 + 0.002 * G1, ...
0 == -0.0033 * M2 + 0.002 * G2, ...
0 == -0.0033 * M3 + 0.002 * G3, ...
0 == 0.1 * M1 - 0.0033 * P1 + 2 * 0.5 * D1 - 2 * 0.025 * (P1 ^ 2), ...
0 == 0.1 * M2 - 0.0033 * P2 + 2 * 0.5 * D2 - 2 * 0.025 * (P2 ^ 2), ...
0 == 0.1 * M3 - 0.0033 * P3 + 2 * 0.5 * D3 - 2 * 0.025 * (P3 ^ 2), ...
0 == -0.5 * D1 + 0.025 * (P1 ^ 2) + 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == -0.5 * D2 + 0.025 * (P2 ^ 2) + 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.5 * D3 + 0.025 * (P3 ^ 2) + 0.9 * (20 - G1) - 0.012 * D3 * G1)
The problem is that I need real solution, but I have got answers with roots. How can I get real answers?
Assuming by "real solution" you mean a numeric value instead of the exact root-of-polynomial form, there are two options depending on how you intend on using the results:
double: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a numeric output of class double. While the output is now limited to double-precision, if your goal is to use the roots in computation and care about performance, this is the fastest option.
vpa: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a Symbolic output of class sym. While the output is now able to be approximated to varying degrees of accuracy at evaluation by calling digits beforehand, there may be a large computational overhead in subsequent calculations due to its Symbolic nature.

Matlab Vectorization : How to avoid this "for" loop?

I have following matrices :
X=1 2 3
Y=4 5 6
A=1 2 3
4 5 6
7 8 9
I Want to do
for each (i,j) in A
v = A(i,j)*X - Y
B(i,j) = v * v'
i.e. each element of A is multiplied by vector X, then resultant vector subtracts Y from itself and finally we take inner product of that vector to bring a single number.
Can it be done without for loop ?
One thing often forgotten in Matlab: The operator ' takes the conjugate transposed (.' is the ordinary transposed). In other words, A' == conj(trans(A)), whereas A.' == trans(A), which makes a difference if A is a complex matrix.
Ok, let's apply some mathematics to your equations. We have
v = A(i,j)*X - Y
B(i,j) = v * v'
= (A(i,j)*X - Y) * (A(i,j)*X - Y)'
= A(i,j)*X * conj(A(i,j))*X' - Y * conj(A(i,j))*X'
- A(i,j)*X * Y' + Y * Y'
= A(i,j)*conj(A(i,j)) * X*X' - conj(A(i,j)) * Y*X' - A(i,j) * X*Y' + Y*Y'
So a first result would be
B = A.*conj(A) * (X*X') - conj(A) * (Y*X') - A * (X*Y') + Y*Y'
In the case of real matrices/vectors, one has the identities
X*Y' == Y*X'
A == conj(A)
which means, you can reduce the expression to
B = A.*A * (X*X') - 2*A * (X*Y') + Y*Y'
= A.^2 * (X*X') - 2*A * (X*Y') + Y*Y'
An alternative method:
X = [1 2 3]
Y = [4 5 6]
A = [1 2 3; 4 5 6; 7 8 9]
V = bsxfun(#minus, A(:)*X, [4 5 6])
b = sum((V.^2)')
B = reshape(b , 3, 3)
I get the result:
B = 27 5 11
45 107 197
315 461 635