Akshay
Akshay

Reputation: 31

Plotting a surface in matlab

I have to plot a surface using three variables x,y and z and these are the functions of three joint angles q1,q2 and q3. but I don't know how to plot this.

for q1=-170:5:170
    for q2=-65:5:140
        for q3=-180:5:70
            c1 = cosd (q1);
            c2 = cosd (q2);
            c3 = cosd (q3);
            s1 = sind (q1);
            s2 = sind (q2);
            s3 = sind (q3);
            x =(320*c1)-(975*c1*s2)-(200*c1*c2*s3)-(200*c1*c3*s2);
            y =(320*s1)-(975*s1*s2)-(200*c2*s1*s3)-(200*c3*s1*s2);
            z = (975*c2)+(200*c2*c3)-(200*s2*s3)+680;
        end
    end
end

Upvotes: 1

Views: 183

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25242

There are some points you need to consider.

  1. the for-loop is not necessary
  2. your coeffcient vectors need to be the same length
  3. you need to use the elementwise-multiplication operator .*

Edit: I actually misunderstood your question first, now it should be correct.

so your code looks like:

q1x = -170:5:170;
q2x = -65:5:140;
q3x = -180:5:70;

[x1, x2, x3] = meshgrid(q1x',q2x',q3x');
Q = [x1(:),x2(:),x3(:)];

q1 = Q(:,1);
q2 = Q(:,2);
q3 = Q(:,3);

c1 = cosd (q1);
c2 = cosd (q2);
c3 = cosd (q3);
s1 = sind (q1);
s2 = sind (q2);
s3 = sind (q3); 
x  = (320.*c1) - (975.*c1.*s2) - (200.*c1.*c2.*s3) - (200.*c1.*c3.*s2);
y  = (320.*s1) - (975.*s1.*s2) - (200.*c2.*s1.*s3) - (200.*c3.*s1.*s2);
z  = (975.*c2) + (200.*c2.*c3) - (200.*s2.*s3)     +  680;

That is quite a big amount of data. - x,y,z are all single points of your surface.

Now I don't really know how your "surface" is supposed to look like. Please clarify what you want to obtain.


scatter3 would give you this cute mushroom:

scatter3(x,y,z,5,z)
colormap(jet)
view(45,10)

mushroom

Getting a real surface from your scatter data is a little tricky, but you can just do some research on "surface from scatter data" or something...

Upvotes: 2

Related Questions