My symbolic polynomial it's not a symbolic polynomial?! Weird error - matlab

I have an symbolic expression like this:
syms h
g=exp(h)+h*exp(h)+h^2*exp(h);
And I really need to extract an polynomial from it, so I wrote:
polyn=coeffs(g,exp(h))
which gives me an symbolic polynomial in h equal to: h^2+h+1.
Now, I want to extract the coefficients from this symbolic polynomial:
coeff=sym2poly(polyn);
But I'm getting the error message:
"Error using symengine (line 58)
Expression is not a polynomial."
So, either has a bug in my computer (which there's a chance to) or Matlab is not recognizing my "symbolic polynomial" as an symb. polyn, actually.
I have to mention that if I type:
polyn=h^2+h+1;
coeff=sym2poly(polyn);
I dont get any error at all! However, since I am programming, I cant type this polynomial, so I need to obtain it in the way I did.
Is there something I can do here??

Here is the solution that works for me:
syms h
g= exp(h)+h*exp(h)+h^2*exp(h);
polyn= coeffs(g,exp(h))
polyn= sym(char(polyn));
coeff= sym2poly(polyn)

Related

Integrating a function on matlab

I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.

MATLAB error when solving simultaneous equations

I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.

How to differentiate a function w.r.t another symbolic function in MATLAB?

Using the code,
syms x(t)
y=x^2
diff(y,t)
diff(y,x)
I get the following error:
2*D(x)(t)*x(t)
Error using sym/diff (line 26)
All arguments, except for the first one, must not be symbolic functions.
Is there a way to tackle this? Thanks for your time.
I dont know much about the Symbolic Math Toolbox, but taking a derivative wrt to a function does not seem to be supported (at least in a direct fashion) for diff.
You can substitute a variable, compute a derivative, and substitute the function back. Like so:
syms z
subs(diff(subs(y,x,z),z),z,x)
ans(t) = 2*x(t)

Integrating and Plotting Polynomials in MATLAB

I have a points for a given polynomial. I would like to integrate, preferably using a definite integral, but I believe in the syntax of using polyint this isn't possible without some manipulation. Regardless, if I can just get it to integrate I'll be able to take it from there.
dpt=coeffvalues(fitresult{4});
ppval=polyval(dpt,xx)
cpdt=coeffvalues(fitresult{2});
cpval=polyval(cpdt,xx)
pint=(ppval./cpval);
intp=polyint(pint);
I've tried doing this a couple of ways...One being fitting the results of the pint curve, finding the coefficients and then using the polyint function. But no matter which way I do it I always get the same three errors:
Error using ./
Matrix dimensions must agree.
Error in polyint (line 16)
pi = [p./(length(p):-1:1) k];
Error in ptintegrate97 (line 61)
intp=polyint(ptint);
Usually its the first error that is causing the problem, but when I do size(ppval) and size(cpval), they are both 837x1. So I'm kinda lost. I'm new to MATLAB sorry if this is a stupid question.
polyint won't work here, because it is expecting a series of polynomial coefficients, but you are providing a series of numbers that are the output of the previous calculation and have no relation to any polynomial coefficients whatsoever. The error you are getting is because the shape of pint is wrong. But even if it was right, you wouldn't get the answer you want.
You can choose to integrate pint numerically if you want. Using a simpson's rule on the pint values could certainly get you to a correct answer if your step size between points is small enough. Or, you could return to doing a symbolic polynomial division in order to get an absolute integral. I am not sure what exactly you are after, or what your requirements are.

Calculating derivative and integration using matlab

I am trying to find second derivative of a function, but while initializing my symbols i am getting the following error:
Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'sym'.
The commands I am using are:
syms x a b c L;
u = (a*x(x-L))+(b*x((x^2)-(L^2)))+(c*x((x^3)-(L^3)));
"u" is my function.
I don't know much about MATLAB's symbolic capabilities, but that error is coming from the pieces like
x(x-L)
which MATLAB is interpreting as an indexing operation. Did you mean multiplication there? I.e.
x*(x-L)