Plotting a multivariate polynomial with sdpvar variables

Suppose I have a polynomial , with degree 6. I want to plot the function where each variable in x runs from to 3.
I saw that using the meshgrid function combined with mesh should be used here. However, when applying:
[x1,x2] = meshgrid(-3:1:3, -3:1:3);
f = replace(f,[x(1) x(2)],[x1 x2])
It is said that "both arguments must have the same size". Here, [x1,x2] returns two 7x7 matrices, and the second line replaces symbolic variables [x(1) x(2)] of x = sdpvar(2,1) with the defined [x1 x2]. Some of these are from the YALMIP toolbox, but I think the core problem remains the same:
How can I set each variable from -3 to 3 and plot the polynomial function?

Answers (1)

Or make f a function handle:
f = matlabFunction(f);
z = f(x1,x2);
surf(x1,x2,z)
"replace" is for strings. This doesn't suffice in the case of symbolic variables.

5 Comments

Both do not work, unfortunately. Are you sure x1 and x2 do not need to be given a number in order to plot?
ezplot gives error:
Input must be a character vector expression, function name, function handle, or INLINE object.
Furthermore, it says ezplot is not recommended.
the function handles gives error:
Check for missing argument or incorrect argument data type in call to function 'matlabFunction'.
Are you sure x1 and x2 do not need to be given a number in order to plot?
But they are given numbers by the meshgrid command.
These two codes work for me:
%First code:
syms x y
f = x^2+y^2;
f = matlabFunction(f);
[x1,x2] = meshgrid(-3:0.1:3, -3:0.1:3);
z = f(x1,x2);
surf(x1,x2,z)
%Second code:
syms x y
f = x^2+y^2;
ezplot(f,[-3,3,-3,3])
Yes this works, but x is not defined by syms but by sdpvar, therefore, according to the workspace, x is a 2x1 sdpvar. By using matlabFunction, I get an error:
Check for missing argument or incorrect argument data type in call to function 'matlabFunction'.
Something like this should do it, but I do not understand the steps:
https://groups.google.com/g/yalmip/c/PXQpXEjhqcA
I don't work with YALMIP, so I can't give you more than this link.

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

DB
on 29 Mar 2022

Edited:

on 30 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!