简体   繁体   中英

Matlab 3d plot custom function

I am new to Matlab and i ran into this problem: I have a function that takes 3 doubles as arguments and outputs a single double eg:

function l = myFunct(a,b,c)
    l = a^2*b^2 + (2*(c^2 - b) / (a - sqrt(c)))
end

Now, I need to plot the result of this function for intervals: a = b = [0.1,3], while keeping c = 2.

I managed to do this for 2d plot of a single variable, but not for 3d...

R = 0:0.01:2;
fun = @(x) myFunct(0.2, x, 3);
B = arrayfun(fun,R);
plot(R, B);

Could you please help and explain?

You can indeed use meshgrid , or ndgrid , to create the two grid arrays. A and B . Then, if your function is not vectorized you need to loop over the entries of A and B . To loop over both at the same time you can use arrayfun . Lastly, you can plot with surf or imagesc .

[A,B] = ndgrid(1.4:0.0001:1.44, -1:.01:3);
Z = arrayfun(@(a,b) myFunct(a,b,2), A, B);
surf(A,B,Z,'edgecolor','none')

在此处输入图片说明

I finally solved it with:

V = 2;

[X,Y] = meshgrid(0.1:0.1:3);
Z = myFunct(X,Y,X*0+V);

figure
surf(X,Y,Z);

Thanks all for the replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM