0

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

Bob van de Voort
  • 211
  • 1
  • 11
  • There is no value of `x` for which `sqrt(x)` returns -3. – Cris Luengo May 25 '18 at 13:47
  • I'm sorry but there is, -3^2=9 3^2=9 thus sqrt(9)= -3 and 3 – Bob van de Voort May 25 '18 at 14:06
  • Yes, but the function `sqrt` always returns positive values. :) – Cris Luengo May 25 '18 at 14:14
  • Yes but this means that matlab can't fully solve all equations, especially when certain equations become more complicated. There is a problem, much more complicated that I need to solve like a 100 times that has a sqrt in it for which I need the negative value to solve it. Mathematically this has solution, so how do I force matlab to find it or how do I work around it and please no I can't go typing it a 100 times since it's much more complicated than just that. – Bob van de Voort May 25 '18 at 15:49
  • Wolfram Alpha agrees with MATLAB on this one: http://www.wolframalpha.com/input/?i=-3+%3D%3D+sqrt(x) – Cris Luengo May 25 '18 at 15:51
  • See also [this answer on Math.SE](https://math.stackexchange.com/a/809427/414894). – Cris Luengo May 25 '18 at 15:57
  • Okay that's all fine and well, but still that doesn't help me solve this problem. No matter what anyone says, mathematically -3^2 is still 9. I'm not here to argue about how we define the positive and negative square root. It is matlabs own notation in a symbolic expression where they use sqrt (where I know it could be both + or -) therefor I want to know how can I get around it. This has to do with solving higher order equations, specifically 2nd order. I understand what you're saying, still if I give it to a highschooler they will solve it for, so my question is how to do it in Matlab. – Bob van de Voort May 25 '18 at 16:33
  • What I am saying is that solving `sqrt(x)==y` is not the same as solving `x==y^2`. If you want the latter, type in the latter. Yes, a high school student will transform the one equation into the other, but I wouldn't leave my math to a high school student. Technically, `x==y^2` is the same as `+/- sqrt(x)==y`. That is, it has two solutions for `y`, and you need to write those two explicitly. `sqrt(x)` is, by definition, a positive value for any real `x`. – Cris Luengo May 25 '18 at 16:47
  • Okay thanks that helps a bit already but it doesn't help me with my final problem unfortunately, nor with the first one. Thanks for the explanation. – Bob van de Voort May 25 '18 at 17:43
  • Although one thing is not clear to me in that case, then why does x^(1/2) have the same issue? – Bob van de Voort May 25 '18 at 17:50
  • I know I haven’t solved your issue, sorry, I don’t have much experience with the symbolic toolbox. The reason `x^0.5` has the same issue is the same reason. A mathematical expression can only have one value. If an expression could have two different values, math would stop working. If `a=b` and `a=c`, then it must be true that `b=c`. Replace `a` with `sqrt(x)`, `b` with 3, `c` with -3, and you see immediately that this goes wrong. Solutions to quadratic equations always use the notation `+/-` to indicate two values. This is just a shorthand for two different expressions. – Cris Luengo May 25 '18 at 22:23

0 Answers0