Thanks much for your time and for all your help.
Actually, I made a mistake in the previous post when specifying the problem. Thus, I reformulate my question using a simpler example. I need to solve symbolically the equation Ct = Z/(P-I) or Ct*(P-I) = Z.
I already know the answer => Ct = [sigma, 1-sigma]
How to program "correctly" the code in order to get the solution
syms sigma;
Ct = sym('Ct',[1 2]);
%
P = [sigma 1-sigma;
sigma 1-sigma];
I = [1 0;
0 1];
Z = [0 0];
%
solve(Ct*(P-I) == Z);
So far, I get :
Z =
0 0
Warning: The solutions are parametrized by the symbols:
z = C_
In solve at 190
In test_matrix_sigma at 13
Or with
solve(Ct == Z/(P-I), Ct);
I get:
Warning: System is rank deficient. Solution is not unique.
Warning: 4 equations in 2 variables.
In /opt/MATLAB/R2013a/toolbox/symbolic/symbolic/symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
In test_matrix_sigma at 13
---------------------------------------------------------------------------------------
Thanks for the answer !
Now I have two issues:
1) When I try to handla a more complicated system:
syms a b P1 P2;
I = [1 0 0 0;
0 1 0 0;
0 0 1 0;
0 0 0 1];
%
P = [a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2)];
%
assume(a, 'real');
assume(b, 'real');
assume(P1, 'real');
assume(P2, 'real');
%
answer = null((P-I)');
disp(answer);
I get
ans =
[ empty sym ]
as the only answer.
2) If there is a way in maltlab to "solve" the above symbolic matrix P and find the symbolic determinant ?
For instance, if I do eid(P) it works;
when I do det(P) it gives 0 as answer...
This post is an answer to a different problem, that was first asked by the OP before being edited. I leave the problem and solution here in case someone ever runs into the same problem:
I need to solve symbolically the following matrix equation to find out Ct (a vector ???):
syms a b P1 P2
%
P = [a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2)];
%
solve(Ct*(P-1) == 0, Ct);
How to proceed ?
So far I get:
Undefined function or variable 'Ct'.
Error in matrix_test (line 10) solve(Ct*(P-1) == 0, Ct);
The error you get is because you did not assign Ct before trying to solve for your equation. In the equation Ct*(P-1) == 0, Matlab does not know what Ct is. You could remedy this by creating a symbolic vector (see sym documentation). For instance:
Ct = sym('Ct', [1 4]);
However, using solve on this would not give you the solutions you're looking for: instead, Matlab is going to give you the trivial answer Ct = 0, which of course is a correct answer to your equation.
What you really want to find is the null space of the (P-1)' matrix: the null space is the set of vectors X such that (P-1)'X = 0 (Which is the same thing as X'(P-1) = 0, so Ct = X'). The Matlab function null (see doc) is what you need. Using your code, I get:
null((P-1)')
ans =
[ -1, 0]
[ 1, 0]
[ 0, -1]
[ 0, 1]
This means that any linear combination of the vectors [-1, 1, 0, 0] and [0, 0, -1, 1] belong to the null space of (P-1)', and therefore its transpose is the Ct you were looking for.
N.B.: This result is easily confirmed by observation of your initial matrix P.
This edited problem is only slightly different from the first one. Once again, you are looking to solve an homogeneous system of linear equations.
The warnings you get simply warn you of that: there are an infinity of solutions ; The first warning tells you that the answer is parameterized by C_ (meaning it's in the complex plane, you could add assume(sigma, 'real') and assume(Ct, 'real') if you wanted, you'd get an answer parameterized by R_.
The solution is to find the null space of the matrix (P-I)', as for the previous problem.
null((P-I)')
ans =
-sigma/(sigma - 1)
1
Now if your vector Z became different from 0, you would need to add the particular solution, that is Z/(P-I). In the present case, it gives:
Z/(P-I)
Warning: System is rank deficient. Solution is not unique.
ans =
[ 0, 0]
This means that in this case the particular solution is [0 0], and the result of null gives you the homogeneous solution. Remember that the complete solution of a linear system of equations is the sum of the particular solution + a linear combination of the elements of the null space. A way to express this in Matlab could be:
syms lambda real;
sol = Z/(P-I) + lambda * null((P-I)')'
sol =
[ -(lambda*sigma)/(sigma - 1), lambda]
Related
When I run this code I get empty strings for when setting a range for vpasolve, when I do not set the range I only get one solution, even with random on.
The range is set so that it does include the one solution matlab gives me, x=2, y=0 and b=1.5.
for the range I've tried -Inf Inf and NaN NaN, to have it try all numbers.
So please do not give me the answer of saying that my system has no solution, it clearly does.
It also won't solve it symbolicly (same issue with solve), while I can give 1 possible solution.
0.5*(x+y)=1, b*(x+y)=3 ---> x+y=2 and b=1.5
So something else must be wrong, I would appreciate it if you let me know what I'm doing wrong here.
clear all; %just to be safe
syms x y b
a=0.5;
somevalue=1;
someothervalue=3;
eq1= a*x+a*y == somevalue; %this is your first equation
eq2= b*x+b*y == someothervalue; %this is your 2nd equation
eqs=[eq1,eq2]; %use this for vpasolve and set range in range
vars=[x,y,b]; %these are the variable you want to solve for
range = [-1 3; -2 5; -Inf Inf]; %NaN means you set no range
%you can use solve or vpasolve, second one being numeric, which is the one you'll probably want
sol=zeros(5,3);
for i = 1:5
temp = vpasolve(eqs, vars, range, 'random', true);
temp = solve(eqs2, vars);
sol(i,1) = temp.x;
sol(i,2) = temp.y;
sol(i,3) = temp.b;
end
sol
temp1.x
temp1.y
temp1.b
Now I have another clear problem/error when using the solve option, obviously the answer here should be 9:
syms x
eq12 = -3 == sqrt(x);
solve(eq12)
ans =
Empty sym: 0-by-1
as well as:
syms x
eq12 = -3 == (x)^(1/2);
solve(eq12)
ans =
Empty sym: 0-by-1
Obviously the answer here would be 9, so what do I change to let matlab solve this and before you tell me to change the equation, this is the equation I would have to change by hand and not just once but a 100 times or so.
More specifically I need to solve something way more complicated which has a solution; negative and positive, the negative value is physically unrealistic but it's the only Matlab can give me.
93659574124777211691008 H
H + ---------------------------------------------
1208925819614629174706176 H + 762832192176831
1
== -----------------
100000000000000 H
29250045579840375 #1
- --------------------------------------------------------------------
H (922337203685477580800 H + 927343445063259249) 4611686018427387904
2
12879770070323045125 #1
+ ----------------------------------------------------------------------
2
55340232221128654848 H (922337203685477580800 H + 927343445063259249)
where
/ 2
| 181939 H 852912375078609598437 H
#1 == 9223372036854775808 H - sqrt| --------- + -----------------------
\ 5539 25544128856069301600256
\
39917248404619332215368770561441 |
+ -------------------------------------- | 9223372036854775808 + 6318009845245521
85070591730234615865843651857942052864 /
The trouble maker is the minus in front of the sqrt I assume
I've been set a question asking me to solve a system of linear equations. In the question it states I should set up a matrix A and column vector b to solve the equation Ax=b, where x is the column vector (w x y z).
A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1]
b = [28;7;22;-4]
A1 = inv(A).*b
sum(A1,2)
This is what I've done so far, however I know the answer that MATLAB gives me is incorrect, as the right solutions should be w=10.5, x=9, y=2.5, z=6.
Can someone point me in the right direction/ show me where I'm going wrong? (I'm fairly new to MATLAB so very unsure about it all).
Thanks.
A = [1 1 1 1; 0 1 4 -2; 2 0 -2 1; 1 -2 -1 1];
b = [28;7;22;-4];
A1 = A \ b;
ans = sum(A1,2);
For a reference concerning the \ operator, please read this: https://it.mathworks.com/help/matlab/ref/mldivide.html
The correct code to use your technique would be:
A1 = inv(A) * b;
but as you may notice, the Matlab code analyzer will point out that:
For solving a system of linear equations, the inverse of a
matrix is primarily of theoretical value. Never use the inverse of a
matrix to solve a linear system Ax=b with x=inv(A)*b, because it is
slow and inaccurate.
Replace inv(A)*b with A\b
Replace b*inv(A) with b/A
and that:
INV(A)*b can be slower than A\b
Suppose that I have the solution vector
conv=(y0,y1,y0+y1,y0+y1+y2,y0+y1+y2+y3);
where y0,y1,y2,y3,y4 are symbolic variables and y2=y0+y1, y3=y0+y1+y2, y4=y0+y1+y2+y3. Now, I assign the values y0=1,y1=1, and then I would like to evaluate recursively (with a for) the solution. Is it possible?
I could solve the problem making
y0=1;
y1=1;
y2=eval(conv(3));
conv(3)=y2;
y3=eval(conv(4));
conv(4)=y3;
y4=eval(conv(5));
conv(5)=y4;
But this is not with a for. It is not the best solution (I know it)
I hope that anyone can help me because the real problem is with 130 variables.
Regards
You need to define your variables as symbolic. Then you can simply use solve:
syms y0 y1 y2 y3 y4
eq = [y0==sym(0),
y1==sym(1),
y2==y0+y1,
y3==y0+y1+y2,
y4==y0+y1+y2+y3]
s = solve(eq)
s.y2
s.y3
s.y4
which returns 1, 2, and 4, respectively.
This system can also be solved numerically using linear algebra with mldivide:
A = [1 0 0 0 0;
0 1 0 0 0;
1 1 -1 0 0;
1 1 1 -1 0;
1 1 1 1 -1];
b = [0;1;0;0;0];
y = A\b
If A and/or b is symbolic, a symbolic linear system solver will be used.
I have three polynomials a(x), b(x) and p(x) over Galois field GF(2^n), and I would like to compute a(x)*b(x) % p(x). Can Matlab compute this expression? So far, I have found this, but it doesn't consider the p(x):
m=n;
a=[1 0 0 0 1 2] % just a example of numbers, the same type arrays for b and p as well
c = gfconv(a,b,m)
It is what I have found after days of searching, but I cannot find anywhere the formula for the type of equation I have.
I think you're looking for remd in this expression (http://nl.mathworks.com/help/comm/galois-fields-of-odd-characteristic.html).
a = gf([1 0 0 0 1 2],n); %your example
b = gf([1 1],n); %just example
p = gf([1 0],n); % just example
[quot,remd] = deconv(conv(a,b),p);
Note that functions gfconv and gfdeconv exist, but Matlab recommends to use standard conv and deconv over 2^n field.
I try with these commands
x = sym('x');
f(x) = sym('f(x)');
f(x) = x/x;
and
f(x) = sym('x/x');
, but both of them produce
f(x) = 1
(yes.. for every real x including 0)
The question is how I can avoid the pre-evaluation in the command "sym",
or there exists another way to handle this problem.
Thank you very much!
update 21.05.2014:
Let me describe the problem a little bit.
Consider
f(x) = x/x
and
g(x) = 1
It is obvious that domains of f and g are R-{0} and R respectively.
The automatic simplification in sym/syms may lead to lose some info.
The answer from #pabaldonedo is good. It seems that the designers of MuPad and the Symbolic Toolbox made a choice as x/x is indeterminate.
If you actually want 0, Inf, -Inf, or NaN, to result in NaN rather than 1 then you can use symbolic variables in conjunction with an anonymous function:
f = #(x)sym(x)./sym(x);
f([-Inf -1 0 1 Inf NaN])
which returns
ans =
[ NaN, 1, NaN, 1, NaN, NaN]
Or, if the input is already symbolic you can just use this:
f = #(x)x./x;
f(sym([-Inf -1 0 1 Inf NaN]))
There is no pre-evaluation in your code. F(x) = x/x is always 1, even for x = 0, Matlab is just simplifying how the function is expressed but there is no pe-evaluation.
I think you should have a look to indeterminate forms to understand why for x = 0, x/x = 1. Have a look to wikipedia: http://en.wikipedia.org/wiki/Indeterminate_form