merlin2011
merlin2011

Reputation: 75555

Why does the following expression result in error?

Disclaimer. I am familiar with Mathematica but not Matlab, so I apologize if this is a neophyte question.

I am getting a strange error on Matlab on the the following at using Matlab's solve command:

solve(0.2 = (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)

The error is:

Error: The expression to the left of the equals sign is not a valid target for an assignment.

The equivalent Solve command in Mathematica (using the same expression) works perfectly, so I don't think my expression itself is invalid. Moreover, I get the same error when I try to use the examples on the doc site: http://www.mathworks.com/help/symbolic/mupad_ref/solve.html

Is it a configuration problem or is there something about the syntax of the command I am misinterpreting?

Edit: I also tried with == instead of =, and I get a different error:

Undefined function or variable 'M'.

Also, as a note, I am running Matlab R2011b (7.13.0.564) 64-bit (glnxa64).

Edit2: I tried the first suggested solution with syms:

>> syms M
>> solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
Error using char
Conversion to char from logical is not possible.

Error in solve>getEqns (line 245)
  vc = char(v);

Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});

Edit3: I have been able to reproduce this issue with even the simplest of equations

>> syms x
>> solve(x^2 -4 == 0, x)
Error using char
Conversion to char from logical is not possible.

Error in solve>getEqns (line 245)
  vc = char(v);

Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});

Moreover, I tried the solution suggested here too: MATLAB examples are failing

Upvotes: 2

Views: 4352

Answers (2)

zachd1_618
zachd1_618

Reputation: 4340

Matlab's fsolve command assumes the expression is set to zero. If solving numerically, you would not do:

x=solve(2=x+1,x)

but rather:

x=fsolve(@(x) x+1-2,0)

Where the equation is already set to zero, @(x) is what you are solving for, and 0 is the initial guess. Which you must include.

Using solve symbolically, it looks like this:

syms x
val=solve(x+1-2)

Or for your system:

syms M
solve(-0.2+ (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))))

ans =
                                     4.7161724968093348297842999805458
                                   0.029173662296926424763929809009225
 - 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
   1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
   1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
 - 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i

Upvotes: 4

bla
bla

Reputation: 26069

you should define M as sym, and use == instead of =

syms M
solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)

ans =
                                         4.7161724968093348297842999805458
                                       0.029173662296926424763929809009225
 - 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
   1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
   1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
 - 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i

Upvotes: 2

Related Questions