Surf a function with different values
显示 更早的评论
I'm trying to surf a function f that takes 2 values: 1 if (x,y) is in A and 2 if (x,y) is in B. How can I do that? I attach also a drawing for x,y.

采纳的回答
Define the function as
f = @(x,y) (y>=2*x) + 2*(y<2*x);
and make a surface plot.
17 个评论
But the function doesn't depend on x and y. It is a constant function. And I want to go further with more triangles dividing the domain. And to make it have a different value within each triangle.
The function has exactly the values you want:
f = @(x,y) (y>=2*x) + 2*(y<2*x);
f(0,1)
ans = 1
f(1,0)
ans = 2
But I admit: Defining a function with prescribed values on each triangle of a triangulation will soon become a hard job. And I really don't understand what it should be good for ...
I want the value to be 1 in every x and y, not just x=0 and y=1.
I've been thinking of this problem for weeks and can't seem to come to some results.
Long story short: I want to calculate the pressure on each triangle from the red domain. The left-hand side of the formula is the pressure on each triangle; the right-hand side is the sum of values of each node from the triangle. The thing is the pressure is constant on each triangle and it has to look like that in the surf. Before it looked like a continuous function which isn't right. And I wanted at first to begin with a simple example.

f = @(x,y) (y>=2*x) + 2*(y<2*x);
x = linspace(0,1,100);
y = linspace(0,2,100);
[X,Y] = ndgrid(x,y);
surf(X,Y,f(X,Y))
shading interp
colorbar

It looks really good but there's a slight problem: what can I modify so I can see only the two triangles in the figure? It must look like a jump from 1 to 2.
Or:
f = @(x,y) (y>=2*x) + 2*(y<2*x);
x = linspace(0,1,100);
y = linspace(0,2,100);
[X,Y] = ndgrid(x,y);
contourf(X,Y,f(X,Y))
colorbar

No, it has to look like the first one without the middle part, in 3D also.
Probably the function has to be modified, I momentarily don't see how.
The marked part must not appear

You might want to play with this representation:
x1 = [0,0,1];
y1 = [0,2,2];
z1 = [1,1,1];
x2 = [0,1,1];
y2 = [0,0,2];
z2 = [2,2,2];
hold on
s1 = fill3(x1,y1,z1,'r');
s2 = fill3(x2,y2,z2,'b');
hold off
view(25,45)
zlim ([0 3])
rotate(s1,[0 0 1],180)
rotate(s2,[0 0 1],180)

Unfortunately that's not going to help me very much because I want to go further and do the same thing but with more triangles like in this case and in each triangle the function to have a different value:

Thank you for all your help!
The method from the link seems to work; however I'm a bit confused with the values of n1, n2 and n3. I don't know what values I should put: if I put [1,1,1] the first coordinates are different, the rest is the same as should be. So I'm guessing the choice of n1, n2 and n3 is wrong.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
