1

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)

I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.

n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f  . exp(- d * pi * r^2) ' , 'r')

The answer I get is:

Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))

> In solve at 190 

ans =

    (fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
   -(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
horchler
  • 18,384
  • 4
  • 37
  • 73
walaa ali
  • 11
  • 1
  • I would recomment you go readup on the documentation for MATLAB, from what I see there is quite a bit of helpful docs – Jaques May 25 '15 at 19:10
  • I'd be curious about the answer it gives, but I don't have MATLAB installed. It would be nice to add this to your question. – Thorbjørn Lindeijer May 25 '15 at 19:55
  • @ThorbjørnLindeijer - I've run the code on my end and I've updated the OP's post. It doesn't make much sense to me either. – rayryeng May 25 '15 at 19:58
  • Here's [the solution](http://www.wolframalpha.com/input/?i=1+%3D+%28%28%CF%80+r2%29^n%29+%2F+n!+%E2%88%99+e^%28-%CF%80+r2%29) for reference; not exactly a nice expression, I'm afraid. – cfh May 25 '15 at 21:18

2 Answers2

2

You have several issues with your code.

1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:

n = sym(500);
f = factorial(n);

which returns an integer approximately equal to 1.22e1134 for f.

2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.

Additionally, as is stated in the R2015a documentation (and possibly earlier):

String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.

If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:

syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)

which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As @BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • I didn't mention that `f` became `Inf` because it wan't being used by the solve call anyway, a problem I considered more relevant. – Ben Voigt May 25 '15 at 21:42
1

A couple things to note:

  1. MATLAB is not using the values of A, d, and f from your workspace.
  2. f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
  3. Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.

You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720