Plot 3d graph with equation and variables

1 view (last 30 days)
I have a function
ypred = 0.3969 - 0.0531*x1 -0.1386*x2 +0.0803*x1.^2 -0.0301*x2.^2 -0.0808*(x1.*x2)
the x1 and x2 values are:
x1 = [0;1;0.5;-0.5;-1;-0.5;0.5]
x2 = [0;0;0.866;0.866;0;-0.866;-0.866]
I plotted the graph using the code attached. This code doesn't seem to work that well because I tried for the same code on a graph that was previously obtained using the equation
ypred = 265 - 167.6*x1 -35.8*x2 +102.9*x1.^2 +47.9*x2.^2 +25.9*(x1.*x2)
It has a minimum point at 0.7940,0.1621. This is not obtained using the code..And the graph doesn't seem right either..

Accepted Answer

Star Strider
Star Strider on 12 Mar 2014
Edited: Star Strider on 13 Mar 2014
The first equation has a minimum far from your x1 and x2 ranges:
ypred1 = @(x) 0.3969 - 0.0531*x(1) -0.1386*x(2) +0.0803*x(1).^2 -0.0301*x(2).^2 -0.0808*(x(1).*x(2));
min_1 = fminsearch(ypred1, [0; 0])
produces:
min_1 =
95.2347e+039
511.6276e+039
The second equation produces a minimum about where you expected it:
ypred2 = @(x) 265 - 167.6*x(1) -35.8*x(2) +102.9*x(1).^2 +47.9*x(2).^2 +25.9*(x(1).*x(2));
min_2 = fminsearch(ypred2, [0; 0])
produces:
min_2 =
794.3380e-003
158.9462e-003
Is this what you want?
ADDED: Using the values for min_2, this seems correct for your second equation (that I called ypred2):
ypred2z = @(x1,x2) 265 - 167.6*x1 -35.8*x2 +102.9*x1.^2 +47.9*x2.^2 +25.9*(x1.*x2);
xvct = -0.5:0.1:2;
[X Y] = meshgrid(xvct, xvct);
Z = ypred2z(X,Y);
figure(1)
meshc(X, Y, Z)
hold on
plot3(min_2(1),min_2(2),val2,'*r')
hold off
xlabel('X_1')
ylabel('X_2')
zlabel('Y_{PRED}')
grid on
  4 Comments
Dilini
Dilini on 13 Mar 2014
Thanks a lot! It makes a lot of sense now :) :)

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!