简体   繁体   中英

Plotting a function in 3D using matlab

I want to plot this function as 3D plots in MATLAB for two cases, can anyone help me out? Thank you.

f(x,y) = (1-x) + ((2x - 1) y / k)

where

case 1)

x = [0,1]  // closed unit interval with real values
y = [0,1]  // closed unit interval with real values
k = 10 is a constant

case 2)

x has domain [0,1] // closed unit interval with real values
y has domain [0,1] // closed unit interval with real values
k has domain [0,1] // closed unit interval with real values

This might help

[X,Y] = meshgrid(0:.1:1);
K = 10;
F = (ones(11) - X) + ((2*X - ones(11)) * Y / K);
figure
mesh(F);

在此处输入图片说明

for case 2

figure
for K 0:.1:1
    F = (ones(11) - X) + ((2*X - ones(11)) * Y / K);
    hold on
    mesh(F);
end

在此处输入图片说明

Try something like the following:

%Case 1:
x = 0:.05:1;
n = numel(x);
y = x';
X = repmat(x,n,1);
Y = repmat(y,1,n);
f = @(x,y) (1-x)+((2*x-1).*y)/10;
v = f(X,Y);
figure;mesh(x,y,v);

%Case 2:
x = 0:.05:1;
n = numel(x);
y = x';
X = repmat(x,n,1);
Y = repmat(y,1,n);
figure;
for k = x
    f = @(x,y) (1-x)+((2*x-1).*y)/k;
    v = f(X,Y);
    hold on;
    mesh(x,y,v);
end

If you want to plot a surface instead of a mesh replace mesh with surf , if you want a finer mesh (more detail and more functions plotted in case 2) change the middle number of x = 0:.05:1; to something smaller like x = 0:.01:1; , and if you want a coarser mesh (less detail and fewer functions plotted in case 2) change the middle number of x = 0:.05:1; to something bigger like x = 0:.1:1; .

I hope this solves your problem!

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