oxfordcontrol / SOSTOOLS

A free MATLAB toolbox for formulating and solving sums of squares (SOS) optimization programs
51 stars 14 forks source link

Is there a way to turn the polynomial into a Matlab function (for instance in order to plot it). #9

Closed hurak closed 1 year ago

hurak commented 1 year ago

Hi, thanks for keeping the toolbox alive.

I only wanted to ask if it is possible to convert the polynomial into a Matlab function. In particular, I would like to plot it. With a symbolic expression I would use matlabFunction, but with the polynomial class, I do not know how to proceed in a simple way (before I start extracting the coefficients and building the function myself).

Thanks.

djagt commented 1 year ago

Hey Hurak,

No problem, glad to hear you found a use for the toolbox!

That's an interesting question. We do not have any way to convert a polynomial to a function handle, I'm also not sure how we would implement that efficiently (though it might be useful). If you just want to evaluate the polynomial at a particular point, you can use double(subs(p,vars,vals)) to substitute the values "vals" of the variables "vars" into the polynomial "p", and convert the result to just a Matlab array using double. For example

pvar x y
p = x^2 + y;
xvals = [1,2,3,4];
yvals = [4,3,2,1];
pvals = double(subs(p,[x;y],[xvals;yvals]));

Is that what you are looking for?

Please let me know if this doesn't answer your question, or if you have any other issues.

djagt commented 1 year ago

Hey Hurak,

I'm going to close this issue. If you feel the problem is not resolved, let me know, and we can start a new issue, or re-open this one.