Matlab surf plot

69 Views Asked by At

===Update: I think I found a solution to the problem. The solution is given below. The original question follows after the solution.

%generating different values for mu and beta to generate surface plot
mu=-1:0.1:1;
beta=-10:1:10;
[X,Y] = meshgrid(mu,beta);
%for loop to calculate ssr for different values of mu and beta
for i=1:11
    for j=1:11
        ssr=0;
        for m=1:3
            ssr = ssr+(yi(m)-(X(i,j)+Y(i,j)*xi(m)))^2;
        end
        Z(i,j) = ssr;
    end
end

%generating the surface plot for ssr values for different mu and beta pairs
figure

surf(X,Y,Z)
xlabel('mu')
ylabel('beta')
zlabel('ssr')

%finding the mu and beta that yield the smallest ssr

[~,q] = min(Z(:));
min_mu = X(q)
min_beta = Y(q)

Please let me know if you think there is some error/inefficiency in this solution.

====End of update/solution.

====My original question is given below:

In Matlab, I am trying to plot the value of sum of squared residuals against a set of parameters (mu, beta). mu takes the values between 0 and 1 and beta takes values between 0 and 10. Sum of squared residuals is calculated using the x,y data pairs.

The formula are:

mu = 0:.1:1;
beta = 0:1:10;

for i = 1:11
    ssr(i) = 0;
    for j = 1:3
        ssr(i) = ssr(i) + (yi(j)-mu(i)-(beta(i)*xi(j)))^2;
    end
end

I am trying to plot ssr (in the z-axis) using mu and beta in the x- and y-axes. I have used the following code:

mat=[mu;beta;ssr]
surf(mat)

I don't get a plot which helps easily visualize the value of ssr for different values of mu and beta. What could I be doing wrong? I would highly appreciate any feedback you could give me. The plot I get is given below: