Undefined Variable in Matlab

404 Views Asked by At

I wrote the following Matlab function:

function $s = Stirling(x,t)$

equation $$ \frac{(s^{(s+0.5)})}{(s-t)^{(s-t+0.5)}}=x.\times exp(t).\times t!$$

$s=$solve (eqn, $s$);

end

Now when I run the command $Stirling(18,3)$ for example, I expect to get a result like $5.7.$

Instead, I get the following error: Undefined function or variable "$s$".

Error in Stirling (line $2)$ eqn $$ \frac{(s^{(s+0.5)})}{(s-t)^{(s-t+0.5)}}=x\times exp(t)\times t!$$

Even when I change my code into:

function $s = Stirling(x,t)$

syms $i;$

eqn $$ \frac{(i^{(i+0.5)})}{(i-t)^{(i-t+0.5)}}=x \times exp(t)\times t!$$

s=solve (eqn, i);

end

And then I run the command Stirling(18,3), I still get the error: Error using solve (line 267) Specify a variable for which you solve.

Error in Stirling (line 4) s=solve (eqn, i);

Does anyone have any suggestions as to how I could force Matlab to recognize "s" as a declared variable?

1

There are 1 best solutions below

4
On

You should define the symbol variable $s$ first, then you can solve it A example code like this may work

function s=stirling(x,t)
syms s
solve (s^(s+0.5)/(s-t)^(s-t+0.5)==x*exp(t)*factorial(t))