Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot (surf) vectorized (Matrix shaped) equation in MATLAB?

Tags:

matlab

surface

I want to surf this function:

z=w.'*p %(close form)

Which:

w=[w0; w1]
p=[cte0; cte1]

There, w is variable and cte0 and cte1 can be any number. To using surf we need to generate w0 and w1 by meshgrid:

[w0,w1]=meshgrid(-50:1:50)

Now we can only reach answer by manipulating equation ( z=w.'*p) to this form:

z=w0*cte0+w1*cte1 %(open form)
surf(z)

I don't want to manipulate my equation, since in other case, bigger equation is not so easy to manipulate them. Is there any way to simply reach surface in this close form other than symbolic toolbox?

like image 475
mohammadsdtmnd Avatar asked Feb 20 '26 01:02

mohammadsdtmnd


1 Answers

So, let's say you start with your "close form" equation as an anonymous function:

z = @(w) w.'*p;

Which assumes p has already been defined and requires an input that is 2-by-1. How will any high-level plotting routines in MATLAB (like fsurf, for example) know that the input should be 2-by-1 (i.e. a set of independent variables [x; y])? They won't, unless you wrap this function in another function like so:

fxy = @(x, y) z([x; y]);

So, this function expects two variables as input, and will concatenate them and pass them in the right form to the function z. But how will the function fxy handle vector or matrix inputs for x or y? Not well, so we'll have to add another layer of wrapping to handle that (courtesy of arrayfun):

fxyMat = @(x, y) arrayfun(fxy, x, y);

Now you have a function that takes vector or matrix inputs for two variables and ultimately evaluates your original, unaltered equation. You can now use fsurf to plot it without explicitly evaluating it at a number of points yourself (basically, you are letting fsurf do it for you):

p = [1; 2];
z = @(w) w.'*p;
fxy = @(x, y) z([x; y]);
fxyMat = @(x, y) arrayfun(fxy, x, y);
fsurf(fxyMat, [-50 50]);

And here's the plot (just a simple tilted plane for this example):

enter image description here

like image 154
gnovice Avatar answered Feb 21 '26 15:02

gnovice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!