简体   繁体   English

Matlab 3D图自定义功能

[英]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: 我是Matlab的新手,但遇到了这个问题:我有一个函数,将3个double作为参数并输出一个double,例如:

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. 现在,我需要绘制此函数在时间间隔上的结果:a = b = [0.1,3],同时保持c = 2。

I managed to do this for 2d plot of a single variable, but not for 3d... 我设法对单个变量的2d图进行了此操作,但对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. 您确实可以使用meshgridndgrid来创建两个网格阵列。 A and B . AB Then, if your function is not vectorized you need to loop over the entries of A and B . 然后,如果函数未向量化,则需要遍历AB的条目。 To loop over both at the same time you can use arrayfun . 要同时循环两者,可以使用arrayfun Lastly, you can plot with surf or imagesc . 最后,您可以使用surfimagesc进行绘制。

[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. 谢谢大家的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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