Surf function problem

I have a problem drawing a surface for a regression.
I have 3 vectors Y, X1, X2 and I have estimated the multiple linear regression: Y = c + a*X1 + b*X2 + u
I would like to draw the graph of this regression: Y = c + a*X1 + b*X2 + u using a MATLAB plot-graph.
I have tried the 'surf' function but I get this error 'Matrix dimensions must agree'.
I have done these things so far.
  1. Estimate the coefficients (c,a,b).
  2. Make a new serie [f=Y(estimated)], using the estimated coefficients (c,a,b). New serie: f = c + a*X1 + b*X2
  3. Use the 'surf' function. surf(X1,X2,f)
Can someone help me? I can’t understand what I am doing wrong.
PS: I use MATLAB 6.5*

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 26 de En. de 2011

1 voto

You'll have to use
meshgrid
to generate your index matrices.
[X1 X2] = meshgrid(1:50);
c = pi;
a = 2;
b = -3;
f = c+a*X1+b*X2;
surf(X1,X2,f)
-Sean

Más respuestas (6)

Sean de Wolski
Sean de Wolski el 26 de En. de 2011

1 voto

I.e. the known Y-values that you used for your regression? How about:
scatter3
or
plot3
to overlay them on your surface?
Sean de Wolski
Sean de Wolski el 26 de En. de 2011

1 voto

Yes.
figure;
surf(...)
hold on
scatter3(...)
strat
strat el 26 de En. de 2011

0 votos

Ohh...I forgot to use the meshgrid... Since X1,X2 have already their values, i didn't know that it needs the meshgrid. Thanks, i will keep in mind. ;) P.S Is there a way to add also the Y vector values at the 3-D graph? :)
strat
strat el 26 de En. de 2011

0 votos

I have done the surface. Thanks again! :) Yep, Y are the known values. So now, how do i use the plot3, scatter3 to add the Y values?
strat
strat el 26 de En. de 2011

0 votos

I think i am doing something wrong...:/ I wrote the code: figure; surf(X1,X2,f) hold on At scatter what i write? :? scatter3(X1,X2,f) ??

1 comentario

Sean de Wolski
Sean de Wolski el 26 de En. de 2011
So you overwrote the original X1,X2 with the meshgrid X1,X2?
The call to scatter3 should be with the _original_ X1,X2 used for your regression and Y
scatter3(X1orig,X2orig,Y)

Iniciar sesión para comentar.

strat
strat el 26 de En. de 2011

0 votos

Nope, i used new series. Nevermind, It seems it had a glitch... Graph and Scatter is fine now.
Thanks a lot man! :thumbsup:

Preguntada:

el 26 de En. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by