How to display a numeric answer instead of a calculation? - matlab

I keep getting answers as a calculation instead of numeric answer, like the following:
(291*pi*((30*3^(1/2))/13 - 1)*((13*3^(1/2)*((30*3^(1/2))/13 - 2))/400 + 197/200))/13
I would rather like to have a numerical answer, i am no MATLAB expert can someone please help me?
Following is the code that leads to this. (All variables except m has been defined earlier)
syms m ;
eqn=(d+m* d* ((sqrt(3))/2)<f);
M = solve(eqn,m);
disp (M)
r= (b/2)+d*((M-1)*sqrt(3)+2)/(4);
L=2* pi* M* w* (r)

Try simplifying the result:
syms m ;
eqn=(d+m* d* ((sqrt(3))/2)<f);
M = solve(eqn,m);
disp (M)
r= (b/2)+d*((M-1)*sqrt(3)+2)/(4);
L=simplify(2* pi* M* w* (r))
EDIT
Since L is a numeric value, the answer is:
L = vpa(2* pi* M* w* (r))

You can also use :
eval(L)
to convert the anser from symbolic to double ( if there isn't a symbolic variable left in the expression).

Related

For binomial function nCr=k, given r and k find n

I need a function that can solve the following: for a binomial function nCr=k, given r and k find n. in mathematics nCr=n!/r!(n-r)!
I tried following but it doesn't solve it. for example 8C6=28, for my function the inputs are 6 and 28 and i want to find 8. This may not have exact integer number so I want to find an x>=n.
"""I am approaching it this way, i.e. find the solution of a polynomial function iteratively, hope there is a better way"""
def find_n(r,k):
#solve_for_n_in(n*(n-1)...(n-r)=math.factorial(r)*k
#in the above example solve_for_n(n*(n-1)(n-2)(n-3)(n-4)(n-5)=720*28)
sum=math.factorial(r)*k
n=r+1
p=1
while p<sum:
p=1
for i in range(0,r+2):
p*=(n-i)
n+=1
return n-1
Thanks.
I solved it the following way, i.e. find the solution of a polynomial function iteratively, hope there is a better way.
def find_n(r,k):
#solve_for_n_in(n*(n-1)...(n-r)=math.factorial(r)*k
#in the above example solve_for_n(n*(n-1)(n-2)(n-3)(n-4)(n-5)=720*28)
target=math.factorial(r)*k
n=r+1
p=1
while p<target:
p=1
for i in range(0,r+2):
p*=(n-i)
n+=1
return n-1
Here's a solution which uses fminsearch. You'll want to minimize the absolute difference between nchoosek(n, r) and k. However, you'll likely run into undefined values for nchoosek, so it's better to define it from scratch. Don't use factorial either though, as it's undefined for negative integers. Instead, use gamma (read about this on Wikipedia if you don't know).
r = 6;
k = 28;
toMinimize = #(n) abs(gamma(n+1) / (gamma(r+1) * gamma(n-r+1)) - k);
Be smart about the initial conditions:
for n = 1:10
[res(n, 1), fval(n, 1)] = fminsearch(toMinimize, n);
end
[res fval]
Now you'll see you should only trust initial conditions n0 >= 5, for which the answer is n = 8.
ans =
1.42626953125 27.9929874410369
1.42626953125 27.9929874410369
3.5737060546875 27.9929874410073
3.57373046875 27.9929874410369
8 0
8.00000152587891 5.2032510172495e-05
8.00000152587891 5.20325100552554e-05
8 0
7.99999694824218 0.000104064784270719
8 0

Solve equation with exponential term

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))
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.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
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')

Derivative of Anonymous Function

I have the following anonymous function:
f = #(x)x^2+2*x+1
I'm using this so that I use it in the following way:
f(0) = 1
But what if I want to find the derivative of such a function while still keeping it's anonymous function capability? I've tried doing the following but it doesn't work:
f1 = #(x)diff(f(x))
but this just returns
[]
Any thoughts on how to accomplish this?
Of course I could manually do this in 3 seconds but that's not the point...
If you have symbolic math toolbox, you can use symbolic functions to achieve the desired as follows:
syms x
myFun=x^2+2*x+1;
f=symfun(myFun,x);
f1=symfun(diff(f),x);
%Check the values
f(2)
f1(2)
You should get 9 and 6 as answers.
When you do diff of a vector of n elements it just outputs another vector of n-1 elements with the consecutive differences.. so when you put a 1 element vector you get an empty one.
A way to go would be to decide an epsilon and use the Newton's difference quotient:
epsilon = 1e-10;
f = #(x) x^2+2*x+1;
f1 = #(x) (f(x+epsilon) - f(x)) / epsilon;
or just do the math and write down the formula:
f1 = #(x) 2*x+2;
http://en.wikipedia.org/wiki/Numerical_differentiation
#jollypianoman this works to me. Actually you need to say that the symfun has to be evaluate using eval command, then you get all the features of an anonymous function. the best is to read the example below...
clear
N0=1;N1=5;
N=#(t) N0+N1*sin(t);
syms t
Ndot=symfun(diff(N(t)),t);
Ndot_t=#(t) eval(Ndot);
Ndot_t(0)
ans = 5
Ndot_t(1)
ans = 2.7015
[tstop] = fsolve(Ndot_t,pi/3)
tstop =
1.5708
cheers,
AP

Integration of a system of differential equations MATLAB

I am a fairly new Matlab user which I had to explore to numerically integrate a system of differential equations. Now I am trying to resolve a simple equation but which gives me a "lambertw" output.
(s - 1) * exp(-s) = k
Therefore, for a given k, with k < exp(2) I should get approximately two different values of "s". Here is the bit of code I use for this task (using symbolic toolbox):
%%Hopf bifurcation calculations
syms s
solve((s-1) * exp(-s) == k, s)
%uhopf = s*k
And the output:
1 - lambertw(0, -(3*exp(1))/25)
After looking at some examples I tried to get an explicit solution with no success:
syms x
x=solve('(s-1)*exp(-s) == k')
Finally, my question is how do I change the result given in the first place into a simple numerical value that fir a given k would give me s1 and s2. Any hint or help would be much appreciated ! I am still looking at some other examples.
If I understand your question correctly, you can use the eval() function to evaluate the string to retrieve a simple numerical example.
e.g.
char s;
char k;
A=solve('(s-1) * exp(-s) = k', 'k=exp(1)');
sol_s=A.s(1);
sol_k=A.k(1);
ans=eval(sol_s)

how to store symbolically the derivatives in matlab

my question relates to the Symbolic Math Toolbox from Matlab. I have the following code:
syms x x_0 u delta sigma_1
mu = sym ('mu(x)');
sigma_u = sym ('sigma(u)');
sigma = sym ('sigma(x)');
f = int (1/sigma_u, u, x_0, x);
df = subs(diff(f,x))
df_2 = subs(diff (f,x,2))
L = subs(mu*df+1/2*sigma^2*df_2)
The result of L is corect
L =
mu(x)/sigma(x) - diff(sigma(x), x)/2
However, for further derivations and for simplicity, I would like to define
sigma_1 = sym('diff(sigma,x)');
or in a similar way such as to get as result for
L =
mu(x)/sigma(x) - sigma_1(x)/2
Basically, I would like to store under a name the symbolic expression diff(sigma(x),x) such that Matlab knows that when it gets this result in a expression, to poste the name sigma_1 (x) instead of diff(sigma(x),x)
Yes it is possible, you can use subs(L, 'diff(sigma(x),x)', 'sigma_1(x)'). Note to make the substitution work, the second input of subs must be exactly like what you want to replace; hence it cannot be 'diff(sigma, x)' which lacks the (x) behind the sigma.
Also note that here is a similar question for which I provided a more complete solution (they asked the question after yours, but I read theirs first).