Solve finds wrong solution? - matlab

I have this equation in x and y:
(x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)) = 0.
Now I call the solver:
R_c = #(y)solve((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, 'Real', true);
which gives me the implicit solutions as a function of y. Now try
R_c(.3)
to find the explicit solution at y = 0.3. MATLAB's answer is:
ans =
0.42846617518653978966562924618638
0.15249587894102346284238111155954
0.12068186494007759990714181154349
However, the last entry in this array is NOT a solution. Test:
double(subs(subs((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, .12068186494007759990714181154349), y, .3))
yields
-0.0585.
This is not a rounding error. The other 2 solutions work perfectly and solve the equation correctly. I wonder where MATLAB the third value gets from. Can anyone help?

Related

How to make the response of the solve function symbolic?

I am solving a fourth order equation in matlab using the solve function.My script looks like this:
syms m M I L Bp Bc g x
m = 0.127
M = 1.206
I = 0.001
L = 0.178
Bc = 5.4
Bp = 0.002
g = 9.8
eqn = ((m + M)*(I + m*L^2) - m^2*L^2)*x^4 + ((m + M)*Bp + (I + m*L^2)*Bc)*x^3 + ((m + M)*m*g*L + Bc*Bp)*x^2 + m*g*L*Bc*x == 0
S = solve(eqn, x)
In the answer, I should get 4 roots, but instead I get such strange expressions:
S =
0
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 1)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 2)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 3)
The first root, which is 0, is displayed clearly. Is it possible to make the other three roots appear as numbers as well? I looked for something about this in the documentation for the solve function, but did not find it.

Using Matlab's Solve() with Symbolic Constants

I'm have a system of two questions with two unknown variables that I'm trying to use Matlab's solve() function to solve, but hitting errors that are almost surely on my end. I'm trying to solve The following two equations where r, lambda, and X are constants. To do so, I've been trying to run the following:
clear all;
syms VA VB r X L;
assumeAlso(VB <= VA);
assumeAlso(0 <= VB);
assumeAlso(0 < r <= 1);
assumeAlso(0 < L);
assumeAlso(0 < X);
assumeAlso(VA, 'real');
assumeAlso(VB, 'real');
assumeAlso(r, 'real');
assumeAlso(L, 'real');
assumeAlso(X, 'real');
eqns = [VA == ((r*VA + L*(VA-VB))^2)/(2*X*(r+2*L)) + (L*(VA+VB)*(r*VA+L*(VA-VB)))/(X*(r+2*L)) + ...
(((r^2/L+r)*VA + r*VB)^2 - (r*VA+L*(VA-VB))^2)/(2*X*(r+L)) + ...
(L*VB*((r+L)*VB - L*VA + r^2/L*VA))/(X*(r+L)) + ...
(X/(2*r)) - (((r^2/L+r)*VA+r*VB)^2)/(2*X*r) - VA*(1-1/X*((r^2/L+r)*VA+r*VB)), ...
...
VB == (L^2*VA^2 - L^2*VB^2 + r*VA*(L*VA+L*VB))/(X*(r+2*L)) + ...
(L*VA*(r^2/L*VA + r*VB - L*(VA-VB)))/(X*(r+L)) + ...
(VA*(X-((r^2/L+r)*VA + r*VB)))/X];
S = solve(eqns,[VA VB])
My goal is to solve for VA and VB in terms of r, X, and L, which seems like it should be possible. When I fill in values for r, L and X I am easily able to obtain a solution.
When I run this code, however, I get the message:
Warning: Explicit solution could not be found.
which normally I would interpret to mean that there is in fact no solution, but when I run
S = solve(eqns)
I do not get the same output, but:
(solvelib::cartesianPower(R_, 2) minus solvelib::VectorImageSet(matrix([[z], [0]]), z, C_)) intersect solvelib::VectorImageSet(matrix([[-(VA^4*x^5 - 2*L^5*VA*VB^3 - 2*L^5*VA^3*VB - L*VA^4*x^4 - 3*L^4*VA^4*x + L^4*VB^4*x + 4*L^5*VA^2*VB^2 - 5*L^2*VA^4*x^3 + 4*L^3*VA^4*x^2 + L^3*VB^4*x^2 + 4*L^2*VA^2*VB^2*x^3 - 5*L^3*VA^2*VB^2*x^2 + 4*L*VA^3*VB*x^4 + L^4*VA*VB^3*x + 9*L^4*VA^3*VB*x + L*VA^2*VB^2*x^4 + 2*L^2*VA*VB^3*x^3 + 2*L^3*VA*VB^3*x^2 - 8*L^3*VA^3*VB*x^2 - 8*L^4*VA^2*VB^2*x)/(L^4*VA^3 - L^4*VB^3 - L^4*VA*VB^2 + L^4*VA^2*VB)], [x]]), x, R_ intersect RootOf(z^6 + (z^5*(L*VA^4 + L*VA^2*VB^2 + 4*L*VA^3*VB))/VA^4 + (z^4*(- 7*L^2*VA^4 + 2*L^2*VA*VB^3 + 8*L^2*VA^3*VB + 6*L^2*VA^2*VB^2))/VA^4 + (z^3*(- 5*L^3*VA^4 + L^3*VB^4 + 6*L^3*VA*VB^3 - 6*L^3*VA^3*VB + 4*L^3*VA^2*VB^2))/VA^4 + (z^2*(7*L^4*VA^4 + 3*L^4*VB^4 + 6*L^4*VA*VB^3 - 2*L^4*VA^3*VB - 14*L^4*VA^2*VB^2))/VA^4 - (z*(6*L^5*VA^4 - 2*L^5*VB^4 - 16*L^5*VA^3*VB + 12*L^5*VA^2*VB^2))/VA^4 + (L^6*VA^4 + L^6*VB^4 - 4*L^6*VA*VB^3 - 4*L^6*VA^3*VB + 6*L^6*VA^2*VB^2)/VA^4, z)) intersect solvelib::VectorImageSet(matrix([[x], [y]]), [x, y], [Dom::Interval(0, Inf), Dom::Interval(0, [1])])
which does not have a solution for VA and VB.
Does anyone have an idea how I can get matlab's solve() to solve for [VA VB] in terms of r, L, and X?
Thank you!
Try using the ReturnConditions flag of the solve command. As in,
S = solve(eqns,[VA,VB],'ReturnConditions',true)
This will return a struct containing an array of solutions and required conditions for each.
I played around a bit with your example and hit the same roadblocks as you when making the same assumptions. If we start from zero assumptions, solve will find a set of solutions for the given equations. You could inspect the conditions for each of the given solutions and see what if any meet your original constraints?
The example given above yields results as long as VA and VB are not constrained to the Reals. Could there be a typo somewhere in your system of eqns?

Simplifying following Boolean Expression and verify using Karnaugh Map

I'm having trouble simplifying these two boolean expressions algebraically and proving them with a Karnaugh Map. How can I do this?
These are my two expressions:
1) (X * Y) + (X' * Y * Z')+ (Y * Z)
2) (X * Y') + Z + (X' + Y)+ (Y * Z)
I've tried going through it using the boolean theorems and laws to reduce them but I always come up with different answers. My answers usually comes up as this.
1) (Y * Z') + (X' * Y)
2) (X' * Y') + (X * Y' * Z')
I don't know if my K-Map is wrong, but I do need someone to help me understand how to solve this problem and the steps or laws I need to get the answer, so that I can master it. It is practice for exam, and I suck at boolean algebra. I appreciate it.
Let's start with the first expression:
E = XY + X'YZ' + YZ
The three terms have Y, then we can factor it out
E = Y(X + X'Z' + Z)
Now let's concentrate in the expression in parenthesis S = X + X'Z' + Z:
S = X + X'Z' + Z
= X + (X + Z)' + Z (De Morgan)
= (X + Z) + (X + Z)' (regrouping)
so, despite the fact that this still looks complex it has the form
S = p + p'
for p = X + Z, right? But p + p' = 1 (or true) no matter the value of p. Thus the expression S is 1 and we get
E = Y(X + X'Z' + Z) = YS = Y1 = Y
In other words, the first expression reduces to Y.
Notice also that it is not that hard to see why S = 1 without rewriting it. There are three cases: (a) If X is true, then certainly the expression is true. (b) If Z is true, the result is true also. (c) If none of X and Z are true then both are false and X'Z' is true. So, in each of these 3 cases at least one of the terms is true, hence their sum.
Let's now consider the second expression
F = XY' + Z + (X' + Y) + YZ
The first thing to note is that XY' is the opposite of (X' + Y):
(X' + Y) = (XY')' (De Morgan)
So,
F = XY' + (XY')' + Z + YZ
Again, regardless of the fact that XY' + (XY')' looks complicated, it is an expression of the form p + p'. But p + p' = 1 (it is always true) and therefore
F = 1 + Z + YZ = 1
no matter the values of Y and Z. So, the second expression is nothing but 1 (aka true).

Can someone explain the simplification of this boolean algebra equation?

I think I missed reading a theorem or postulate or something..
The equation is: wy + wxz + xyz
According to my professor, the simplification is (which she didn't explain how):
wy + xz(w'y + wy')
= wy + xz (w XOR y)
Where did that (w'y + wy') came from??
I tried to calculate it but so far I only got: (w+x)(w+y)(w+z)(x+y)(y+z)
In a Boolean expression + is XOR and * is AND. This is the same as identifying true with 1 and false with 0, with the only special convention that 1 + 1 = 0 (or 2 = 0 if you wish.)
With these definitions both + and * are commutative, i.e., a + b = b + a and a * b = b * a. In addition, the distributive law is also valid a * (b + c) = a * b + a * c. Note also that the * operator is usually implicit in the sense that we write ab instead of a * b.
Applying these properties to the expression wy + wxz + xyz, there are some few obvious transformations you can do:
wy + wxz + yxz (commute x with y)
wy + (w + y)xz (prev plus distribute xz)
wy + xz(w + y) (prev plus commute (w + y) with xz
Note that the last one is wy + xz(w XOR y) because + is nothing but XOR.
ADDENDUM
Regarding the expression of your professor, let's recall that a' = 1 - a by definition. So
w'y + wy' = (1 - w)y + w(1 - y) - def
= y - wy + w - wy - distribute
= y + w - simplify (a + a = 0 always)
= w + y - commute
which shows that s/he was right.

using quad to integrate function with respect to just one variable

is there any way i can integrate function of two variable, say
f=#(x) x^2 + x*y
over just x
tried quad(f, a, b)
but doesn't work, looking for alternative solution
Looks like you want something like this:
y = 100; % whatever y is
a = 0;
b = 2;
% you'll need to vectorize the integrand function
f = #(x) x.*x + x.*y
val = quad(f, a, b);
However, if you are looking for an algebraic answer, you'll need to use the Symbolic Toolbox, or some other software, or your calculus book. :-)
The whole "vectorize" thing comes from the Mathworks quad documentation that says:
The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.
Sorry, but quad does not solve symbolic problems. It does only numerical integration.
syms x y
int(x^2 + x*y,x)
ans =
(x^2*(2*x + 3*y))/6
The natural way to solve a symbolic problem is to use a symbolic tool.
From the followup, Anya wants something in-between. To steal the words of an old rock star named Mick, "You can't always get what you want."
Again, quad can't be used if you wish to integrate ONLY over x, as quad is an adaptive tool.
In SOME SIMPLE cases, you can use a simple tool like Simpson's rule to do the work. For example, suppose you wanted to solve the above problem, with x integrated over the interval [0 1]. For purposes of comparison, I'll do it symbolically first.
syms x y
res = int(x^2 + x*y,x);
subs(res,x,1) - subs(res,0)
ans =
y/2 + 1/3
Now, lets try it using a numerical integration on x.
syms y
x = 0:.01:1;
coef = mod((0:100)',2)*2 + 2;
coef([1 end]) = 1;
coef = 0.01*coef/3;
(x.^2 + x.*y)*coef
ans =
y/2 + 1/3
So in this very SIMPLE case, it did work. How about something a little more complicated? Integrate x*exp(x*y) over the interval [-1 1]. Again, a known form is accessible symbolically.
syms x y
res = int(x*exp(x*y),x);
res = subs(res,x,1) - subs(res,-1)
res =
(exp(-y)*(y + 1))/y^2 + (exp(y)*(y - 1))/y^2
To test it out later, what value does this take on at y = 1/2?
vpa(subs(res,y,1/2))
ans =
0.34174141687554424792549563431876
Lets try the same trick, using Simpson's rule.
syms y
x = -1:.01:1;
coef = mod((-100:100)',2)*2 + 2;
coef([1 end]) = 1;
coef = 0.01*coef/3;
res = (x.*exp(x*y))*coef
res =
exp(y/2)/300 - exp(-y/2)/300 - exp(-y)/300 - exp(-y/4)/300 + exp(y/4)/300 - exp(-y/5)/750 + exp(y/5)/750 - exp(-(3*y)/4)/100 - exp(-(2*y)/5)/375 + exp((2*y)/5)/375 + exp((3*y)/4)/100 - exp(-(3*y)/5)/250 + exp((3*y)/5)/250 - (2*exp(-(4*y)/5))/375 + (2*exp((4*y)/5))/375 - exp(-y/10)/1500 + exp(y/10)/1500 - exp(-(3*y)/10)/500 + exp((3*y)/10)/500 - (7*exp(-(7*y)/10))/1500 + (7*exp((7*y)/10))/1500 - (3*exp(-(9*y)/10))/500 + (3*exp((9*y)/10))/500 - exp(-y/20)/1500 + exp(y/20)/1500 - exp(-(3*y)/20)/500 + exp((3*y)/20)/500 - exp(-y/25)/3750 + exp(y/25)/3750 - (7*exp(-(7*y)/20))/1500 - exp(-(2*y)/25)/1875 + exp((2*y)/25)/1875 + (7*exp((7*y)/20))/1500 - exp(-(3*y)/25)/1250 + exp((3*y)/25)/1250 - (3*exp(-(9*y)/20))/500 - (2*exp(-(4*y)/25))/1875 + (2*exp((4*y)/25))/1875 + (3*exp((9*y)/20))/500 - (11*exp(-(11*y)/20))/1500 - exp(-(6*y)/25)/625 + exp((6*y)/25)/625 + (11*exp((11*y)/20))/1500 - (7*exp(-(7*y)/25))/3750 + (7*exp((7*y)/25))/3750 - (13*exp(-(13*y)/20))/1500 - (4*exp(-(8*y)/25))/1875 + (4*exp((8*y)/25))/1875 + (13*exp((13*y)/20))/1500 - (3*exp(-(9*y)/25))/1250 + (3*exp((9*y)/25))/1250 - (11*exp(-(11*y)/25))/3750 + (11*exp((11*y)/25))/3750 - (17*exp(-(17*y)/20))/1500 - (2*exp(-(12*y)/25))/625 + (2*exp((12*y)/25))/625 + (17*exp((17*y)/20))/1500 - (13*exp(-(13*y)/25))/3750 + (13*exp((13*y)/25))/3750 - (19*exp(-(19*y)/20))/1500 - (7*exp(-(14*y)/25))/1875 + (7*exp((14*y)/25))/1875 + (19*exp((19*y)/20))/1500 - (8*exp(-(16*y)/25))/1875 + (8*exp((16*y)/25))/1875 - (17*exp(-(17*y)/25))/3750 + (17*exp((17*y)/25))/3750 - (3*exp(-(18*y)/25))/625 + (3*exp((18*y)/25))/625 - (19*exp(-(19*y)/25))/3750 + (19*exp((19*y)/25))/3750 - (7*exp(-(21*y)/25))/1250 + (7*exp((21*y)/25))/1250 - (11*exp(-(22*y)/25))/1875 + (11*exp((22*y)/25))/1875 - (23*exp(-(23*y)/25))/3750 + (23*exp((23*y)/25))/3750 - (4*exp(-(24*y)/25))/625 + (4*exp((24*y)/25))/625 - exp(-y/50)/7500 + exp(y/50)/7500 - exp(-(3*y)/50)/2500 + exp((3*y)/50)/2500 - (7*exp(-(7*y)/50))/7500 + (7*exp((7*y)/50))/7500 - (3*exp(-(9*y)/50))/2500 + (3*exp((9*y)/50))/2500 - (11*exp(-(11*y)/50))/7500 + (11*exp((11*y)/50))/7500 - (13*exp(-(13*y)/50))/7500 + (13*exp((13*y)/50))/7500 - (17*exp(-(17*y)/50))/7500 + (17*exp((17*y)/50))/7500 - (19*exp(-(19*y)/50))/7500 + (19*exp((19*y)/50))/7500 - (7*exp(-(21*y)/50))/2500 + (7*exp((21*y)/50))/2500 - (23*exp(-(23*y)/50))/7500 + (23*exp((23*y)/50))/7500 - (9*exp(-(27*y)/50))/2500 + (9*exp((27*y)/50))/2500 - (29*exp(-(29*y)/50))/7500 + (29*exp((29*y)/50))/7500 - (31*exp(-(31*y)/50))/7500 + (31*exp((31*y)/50))/7500 - (11*exp(-(33*y)/50))/2500 + (11*exp((33*y)/50))/2500 - (37*exp(-(37*y)/50))/7500 + (37*exp((37*y)/50))/7500 - (13*exp(-(39*y)/50))/2500 + (13*exp((39*y)/50))/2500 - (41*exp(-(41*y)/50))/7500 + (41*exp((41*y)/50))/7500 - (43*exp(-(43*y)/50))/7500 + (43*exp((43*y)/50))/7500 - (47*exp(-(47*y)/50))/7500 + (47*exp((47*y)/50))/7500 - (49*exp(-(49*y)/50))/7500 + (49*exp((49*y)/50))/7500 - exp(-y/100)/7500 + exp(y/100)/7500 - exp(-(3*y)/100)/2500 + exp((3*y)/100)/2500 - (7*exp(-(7*y)/100))/7500 + (7*exp((7*y)/100))/7500 - (3*exp(-(9*y)/100))/2500 + (3*exp((9*y)/100))/2500 - (11*exp(-(11*y)/100))/7500 + (11*exp((11*y)/100))/7500 - (13*exp(-(13*y)/100))/7500 + (13*exp((13*y)/100))/7500 - (17*exp(-(17*y)/100))/7500 + (17*exp((17*y)/100))/7500 - (19*exp(-(19*y)/100))/7500 + (19*exp((19*y)/100))/7500 - (7*exp(-(21*y)/100))/2500 + (7*exp((21*y)/100))/2500 - (23*exp(-(23*y)/100))/7500 + (23*exp((23*y)/100))/7500 - (9*exp(-(27*y)/100))/2500 + (9*exp((27*y)/100))/2500 - (29*exp(-(29*y)/100))/7500 + (29*exp((29*y)/100))/7500 - (31*exp(-(31*y)/100))/7500 + (31*exp((31*y)/100))/7500 - (11*exp(-(33*y)/100))/2500 + (11*exp((33*y)/100))/2500 - (37*exp(-(37*y)/100))/7500 + (37*exp((37*y)/100))/7500 - (13*exp(-(39*y)/100))/2500 + (13*exp((39*y)/100))/2500 - (41*exp(-(41*y)/100))/7500 + (41*exp((41*y)/100))/7500 - (43*exp(-(43*y)/100))/7500 + (43*exp((43*y)/100))/7500 - (47*exp(-(47*y)/100))/7500 + (47*exp((47*y)/100))/7500 - (49*exp(-(49*y)/100))/7500 + (49*exp((49*y)/100))/7500 - (17*exp(-(51*y)/100))/2500 + (17*exp((51*y)/100))/2500 - (53*exp(-(53*y)/100))/7500 + (53*exp((53*y)/100))/7500 - (19*exp(-(57*y)/100))/2500 + (19*exp((57*y)/100))/2500 - (59*exp(-(59*y)/100))/7500 + (59*exp((59*y)/100))/7500 - (61*exp(-(61*y)/100))/7500 + (61*exp((61*y)/100))/7500 - (21*exp(-(63*y)/100))/2500 + (21*exp((63*y)/100))/2500 - (67*exp(-(67*y)/100))/7500 + (67*exp((67*y)/100))/7500 - (23*exp(-(69*y)/100))/2500 + (23*exp((69*y)/100))/2500 - (71*exp(-(71*y)/100))/7500 + (71*exp((71*y)/100))/7500 - (73*exp(-(73*y)/100))/7500 + (73*exp((73*y)/100))/7500 - (77*exp(-(77*y)/100))/7500 + (77*exp((77*y)/100))/7500 - (79*exp(-(79*y)/100))/7500 + (79*exp((79*y)/100))/7500 - (27*exp(-(81*y)/100))/2500 + (27*exp((81*y)/100))/2500 - (83*exp(-(83*y)/100))/7500 + (83*exp((83*y)/100))/7500 - (29*exp(-(87*y)/100))/2500 + (29*exp((87*y)/100))/2500 - (89*exp(-(89*y)/100))/7500 + (89*exp((89*y)/100))/7500 - (91*exp(-(91*y)/100))/7500 + (91*exp((91*y)/100))/7500 - (31*exp(-(93*y)/100))/2500 + (31*exp((93*y)/100))/2500 - (97*exp(-(97*y)/100))/7500 + (97*exp((97*y)/100))/7500 - (33*exp(-(99*y)/100))/2500 + (33*exp((99*y)/100))/2500 + exp(y)/300
So I got a result, but its not the analytical one I wanted, and a bit of a nasty mess. Is it correct?
vpa(subs(res,y,1/2))
ans =
0.34174141693463006644516447861307
I'll copy the analytical result from above so we can compare...
0.34174141687554424792549563431876
As you can see, Simpson's rule, at a step size of 0.01 over [-1,1], did reasonably well, agreeing out to about 9 decimal digits.
There is no assurance that this technique will work as well on any more general kernel, but it might give you what you desire.