tags:

views:

51

answers:

2

Hi, 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;
+2  A: 

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, '+')
Richie Cotton
A: 

@Richie Cotton: Thanks, but 1) isn't meshgrid unecessary here? It seems to plot fine without it, no change. 2) For my values, x and y have the same dimensions so when I plot, it graphs only on the diagonal which is not actually the case (the values for f(x,y) are actually all over the place but I cant get the plot to show this!). Any suggestions?

John
@John: `meshgrid` is necessary for the general case. If `x` and `y` are different lengths, then you can't add them together. If you want more help, you'll have to provide a minimal, reproducible example.
Richie Cotton

related questions