开发者

3D scatter plots in Matlab

开发者 https://www.devze.com 2023-01-05 20:32 出处:网络
I\'m fairly new to Matlab and would appreciate some help. I\'m trying to graph a scatter plot of a function. Later on I\'m going to fit other functions to this data and plot that on the same figure. B

I'm fairly new to Matlab and would appreciate some help. I'm trying to graph a scatter plot of a function. Later on I'm going to fit other functions to this data and plot that on the same figure. But what I have so far plots the markers all in one plane, which isn't what I want. The function is 2D, the graph should be 3D. How do I get this?

Here's what I've been trying so far. There's some other code before this that generates different values for f(i,j) given different parameters, so when I implement the code I get a series of figures.

for i=1:somenumber
    for j=1:somenumber
        f(i,j)=etc.
    开发者_高级运维end
end

figure;
x=1:somenumber;
plot3(x,f,x,'rs');
hold on;


See my comment on why you likely don't want to do this, but the general way of plotting in 3D is

x = 1:10;
y = 1:5;
[X Y] = meshgrid(x, y);
Z = X.^2 + 2 .* Y;      % in general, Z = f(X, Y)
plot3(X, Y, Z, '+')


Here is an avenue worth exploring:

nSamples = nX * nY;
xValues  = zeros( nSamples, 1 );
yValues  = zeros( nSamples, 1 );
zValues  = zeros( nSamples, 1 );

iSample = 0;
for iX = 1:nX
   for iY = 1:nY
      iSample = iSample + 1;

      xValues( iSample ) = iX;
      yValues( iSample ) = iY;
      zValues( iSample ) = someFunction( iX, iY );

   end
end

figure;
plot3( xValues(:), yValues(:), zValues(:), 'r.' );

This should make it easy to add noise to any or all of x, y or z to test your function fitting algorithm.

0

精彩评论

暂无评论...
验证码 换一张
取 消