Let
syms h
g=exp(h)+h*exp(h)+h^2
so, the coefficients of g in respect to his given by the function coeffs:
coeffs(g,h)
and it returns:
[exp(h),exp(h),1]
It's like this function deals with the symbolic expression like a polynomial in h.
The problem is that this function doesnt return the zero coefficients, so if I have an
g=h*exp(h)+h^2
the function returns only:
[exp(h), 1].
But what I need is:
[0,exp(h), 1]
So, what can I do here?
I went to a lot of topics on SO and the solution is:
syms h
g=h*exp(h)+h^2
m = eval(feval(symengine,'coeff',g,h,'All'));
I hate to be one of those posters that lectures, but this is listed right in the Matlab help (help coeff) or online
coeffs(g,h, 'All')
Related
I want to solve the following optimisation problem, whose objective function is independent of x:
minimise t
subject to a_i*x+b_i<=t
over x for all i from 1 to n=100
This problem arised from if I rewrite a piecewise linear optimisation problem to a linear optimisation problem.
Question:
How can I implement this in the matlab using linprog. In linprog I am asked to insert the objective function f as a matrix multiplying with x. Is it possible to have a objective function independent of x? If not, how am I going to implement this?
P.S: I don't know why Mathjax is not working here, I have been looking for how to ask questions with Math formula, but I was not successful. Therefore any correction is welcomed.
Edit:Here is the official document from Matlab for linprog. In it it is stated that the minimalising function has the shape of f^Tx. My problem is just that my minimalising function doesn't take such shape and is in the absence of x. How can I implement this in the Matlab?
You need to reformulate your problem into standard LP, i.e. find the nominal f, A and b as required by MATLAB's function linprog.
Here is the reformulation. From your question it seems x and t are two scalars.
Assume the given a_i's and b_i's are stored in column vectors a and b respectively, then your original problem:
min t, s.t. a * x + b <= t * ones(n,1)
is equivalent to:
min [0; 1]' * [x; t], s.t. [a, ones(n,1)] * [x; t] <= -b.
Thus, just use linprog([0; 1], [a, ones(n,1)], -b), and the solution is [x; t].
I'm trying to solve equation f for r, as below:
syms rho
C0 = 0.5;
a_bar = sqrt(-log((1-C0)/(1+C0)));
l = 0.77;
f = #(r) exp(-r^2)*int(rho*exp(-rho^2)*besseli(0,2*r*rho),rho,0,a_bar)-(l-1)*int(rho*exp(-rho^2),rho,0,a_bar);
r1 = fzero(f,1);
However the symbolic output from the first integral (containing besseli) is giving errors in fzero. The problem seems to be that besseli contains rho (when I remove this instance of rho I don't get any errors).
I've tried playing with subs and eval, and solving the entire thing as symbolic, but it's really been trial and error to be honest. I'm sure there's something simple that I'm missing - any help would be amazing!
Cheers,
Alan
As suggested by David in the comments to my question, including double within the function solves this problem; i.e:
f = #(r) double(exp(-r^2)*int(rho*exp(-rho^2)*besseli(0,2*r*rho),rho,0,a_bar)-(l-1)*int(rho*exp(-rho^2),rho,0,a_bar));
r1 = fzero(f,1);
This works as it converts the symbolic expression (involving rho and r) into a numerical object. My equation doesn't have any roots but that's an issue with my working beforehand.
Thanks again to David and Mad Physicist for the help on this.
My question refers to the Matlab symbolic toolbox.
I'm trying to derive a symbolic function that is a function of another symbolic function. Say that I have a function x that is an unspecified function x=x(y(theta)). I'd like to take the derivative of x with respect to theta: dx/dtheta=dx/dy * dy/dtheta
In Matlab I write
syms theta y(theta);
x=sym('x(y(theta))');
diff(x,theta)
The answer I get is 0. I really cannot figure out what is wrong with the code.
Any help is greatly appreciated. Thanks!
Derivating f(g(y)):
syms x,y
f = symfun(sym('f(x)'), [x])
g = symfun(sym('g(y)'), [y])
diff(f(g(y)),y)
Is it possible to get the derivative of a function_handle as a other function_handle?
Like:
fun1 = #(x) x^2;
% do that ...
disp(fun2);
#(x) x*2
I know how to find the derivative of a symbolic function but I can't convert a function_handle to a symbolic function.
I'm new to matlab and I couldn't find any way on doing that. Thanks in advance.
The short answer is "No." MATLAB has no idea what the contents of the function_handle mean in a symbolic sense. You're better off creating it using syms in first place.
A longer answer would be either to use the Symbolic Math Toolbox, as suggested by #A Danesh, or an approximation, as suggested by #Andrey.
However, if you're always working with polynomials, then you can store the coefficients as an array and use the following functions:
polyval to evaluate
conv to multiply
deconv to divide
polyder to differentiate
polyint to integrate
syms x
f = #(x) x^2 + 1;
diff(f,x)
Ans:
2*x + 1
You can get an approximation function:
delta = 0.0001;
d = #(x)( (fun1(x+delta) - fun1(x))./delta)
you can't analytically from a function handle.
but if you got the symbolic math toolbox you can derivate the symbolic function and create a function handle from the result.
I have a system of 2 equations in 2 unknowns that I want to solve using MATLAB but don't know exactly how to program. I've been given some information about a gamma distribution (mean of 1.86, 90% interval between 1.61 and 2.11) and ultimately want to get the mean and variance. I know that I could use the normal approximation but I'd rather solve for A and B, the shape and scale parameters of the gamma distribution, and find the mean and variance that way. In pseudo-MATLAB code I would want to solve this:
gamcdf(2.11, A, B) - gamcdf(1.61, A, B) = 0.90;
A*B = 1.86;
How would you go about solving this? I have the symbolic math toolbox if that helps.
The mean is A*B. So can you solve for perhaps A in terms of the mean(mu) and B?
A = mu/B
Of course, this does no good unless you knew B. Or does it?
Look at your first expression. Can you substitute?
gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) = 0.90
Does this get you any closer? Perhaps. There will be no useful symbolic solution available, except in terms of the incomplete gamma function itself. How do you solve a single equation numerically in one unknown in matlab? Use fzero.
Of course, fzero looks for a zero value. But by subtracting 0.90, that is resolved.
Can we define a function that fzero can use? Use a function handle.
>> mu = 1.86;
>> gamfun = #(B) gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) - 0.90;
So try it. Before we do that, I always recommend plotting things.
>> ezplot(gamfun)
Hmm. That plot suggests that it might be difficult to find a zero of your function. If you do try it, you will find that good starting values for fzero are necessary here.
Sorry about my first try. Better starting values for fzero, plus some more plotting does give a gamma distribution that yields the desired shape.
>> B = fzero(gamfun,[.0000001,.1])
B =
0.0124760672290871
>> A = mu/B
A =
149.085442218805
>> ezplot(#(x) gampdf(x,A,B))
In fact this is a very "normal", i.e, Gaussian, looking curve.