1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MatLab: About the basic functions about the program.

  1. Mar 14, 2014 #1
    Hi.

    I've been given an assignment, a starters assignment I should say, for Matlab. This is mostly about learning the basic functions of the program and it's my first ever experience with it.

    I hope I'm posting in the correct sub-forum.

    1. The problem statement, all variables and given/known data

    We're given a triangle ABC, where A(1,3), B(­2,0) and C(2,­4). The triangle can be drawn with the following code:

    Code (Text):
    % triangle_ABC.m
    clear all
    P = [ 1 -2 2 1
     3 0 -4 3 ];
    plot(P(1,:),P(2,:),'-b'), axis equal
    a)

    rotate the triangle 30 degrees in negative direction around origo (does this even make sense? Trying to translate it as best as I can). Draw the new triangle in the same coordinate system (as the original triangle I assume) - with another color.

    b)

    Mirror the rotated triangle around the x-axis. Draw the new triangle in the same coordinate system with a 3rd color.

    c)

    Scale the rotated triangle to double size. Draw the new triangle in the same coordinate system with a 4th color.

    d)

    Study how many different triangles you can end up with by changing the order of the three transformations.

    2. Relevant equations

    Matrices. Rotation, scaling and mirror matrices.

    3. The attempt at a solution

    Alright, first things first. I think it's best to start with exercise a, and then move on from there - I might be able to finish them myself when I've learned how to do the first one.

    So, for the rotating part.

    I will need a matrix to rotate the triangle:

    Code (Text):
    theta =30;% degree of rotation counterclockwise
    Mrot = [ cosd(theta) -sind(theta)
     sind(theta) cosd(theta) ]
    Then I get these values:

    Mrot =

    0.8660 -0.5000
    0.5000 0.8660

    This is where I'm stuck. To get my new coordinates, do I multiply my coordinates with this matrix? If this is the answer, how do I do this in matlab?

    Any type of input here will be greatly appreciated.

    Thanks in advance.
     
  2. jcsd
  3. Mar 14, 2014 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Around the origin.

    Yes. Just use the multiplication sign for matrix (and matrix&vector) multiplications.
     
  4. Mar 15, 2014 #3
    Thanks for getting back to me. I apologize the slow reply, but I didn't have matlab on my own computer, but now I do. I will therefore try out solutions and reply straight away now.

    Remember that this is my first ever experience with matlab and I have to use google to figure out pretty much every command (for some commands, trial and error works fine heh).

    So to plot my first graph, I wrote this:

    Code (Text):
    % triangle_ABC.m
    clear all
    P = [ 1 -2 2 1
     3 0 -4 3 ];
    plot(P(1,:),P(2,:),'-b'), axis equal
    Then I used the rotation matrix, and got this:

    Mrot:

    0.8660 -0.5000
    0.5000 0.8660

    I multiplied the matrix for the original triangle:

    [1 -2 2; 3 0 -4]

    with Mrot, and got this:

    -0.6340 -1.7320 3.7320
    3.0980 -1.0000 -2.4640

    Then I tried plotting that in like so:

    Code (Text):
    Q = [ -0.6340   -1.7320    3.7320 -0.6340
     3.0980   -1.0000   -2.4640 3.0980 ];
    plot(Q(1,:),Q(2,:),'-b'), axis equal
    But nothing happened.. I've tried google'ing how to draw multiple triangles in matlab but can't seem to find anything that fits my formula of drawing (and that's the one I have to use).

    So my questions are: how to I plot multiple triangles into the same coordinate system - and how can I change the color?

    Also, should I multiply the whole matrix for the original triangle, or should I do the vectors separated? Like coordinates for A * Mrot. Coordinates for B * Mrot and then coordinates for C * Mrot?

    Thanks a lot for helping me out with this.
     
  5. Mar 15, 2014 #4

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    I use Matlab to handle some measurement values, but not for plotting, I have no idea how that works. The manual should help, and it comes with examples.
     
  6. Mar 15, 2014 #5
    That's totally understandable.

    What I have done is checked other problems on the internet - and you're probably right that it's in the manual. What I found out was that there's a hold command, so I plot my graphs using this code:

    Code (Text):
    P = [ 1 -2 2 1
     3 0 -4 3 ];
    plot(P(1,:),P(2,:),'-b')[B],hold on[/B], axis equal
    So, the blue triangle is the original, the red one is the one that's rotated 30 degrees in negative direction around the origin. Does the attached picture look correct? It should, but I can't think clearly after being frustrated with this simple problem for a couple of days.

    Edit: Of course it looks right, looking that the formulas I've used and how the triangles look. Unless I'm completely wrong here, I'm moving on to my next exercise.
     

    Attached Files:

    Last edited: Mar 15, 2014
  7. Mar 15, 2014 #6

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    I don't see the original positions A,B,C in the plot. Where is C(2,4) for example?

    Edit: Ah, probably a typo in the first post. But then the rotation direction looks wrong.
     
  8. Mar 15, 2014 #7
    You're absolutely correct. I forgot the minus sign in front of theta = -30.

    But even so, my mirror triangle, that should be mirror'ed around the x-axis seems wrong. I'm using

    Mmir = [ 1 0
    0 -1 ]

    and

    R = Mmir*P

    R =

    1 -2 2 1
    -3 0 4 -3


    plot(R(1,:),R(2,:),'-c')

    But then I end up with the attached triangles. (I've now fixed the rotation direction)

    Everything seems correct in my code. Surely, the R matrix is P matrix mirrored? Original is blue, rotated is red. Last one is the "mirrored" triangle.
     

    Attached Files:

  9. Mar 15, 2014 #8

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Cyan is the blue triangle, mirrored at the x-axis.
    Red is the blue triangle, rotated by -30 degrees.

    Looks correct.
     
  10. Mar 15, 2014 #9
    Of course. I'm not sure what I was thinking - or wasn't thinking. I tend to overlook the simplest things when learning new things - like learning how to use matlab now. I will try to focus more in the future.

    Thanks a lot for helping me out, even with the simplest things. I will probably be back tomorrow with new questions, but I want to go through them once more, just to see if I've made similar "mistakes".

    Appreciate the reply mate.
     
  11. Mar 16, 2014 #10
    For my second problem, I'm supposed to plot the graph of the function f(x) = arctan(x) * e-x^2 - [itex]\frac{x}{2}[/itex] where -2<=x<=2.

    To do this in Matlab, I've written this code:

    Code (Text):
    x = [ -2 -1 0 1 2 ] %the boundaries for the function.

    y = atan(x) .* e(1).^(-x.^2)-(x/2) %the y funxtion.

    This is my error message: Error using *
    Inner matrix dimensions must agree.

    In my example, this is the first two steps.

    What am I doing wrong here?

    Edit: sorry for the double post. Didn't know I had posted last.

    Edit 2: I updated my code to this:

    Code (Text):
    x = [ -2 -1 0 1 2 ] %the boundaries for the function.

    y = atan(x) .* e(1).^(-x.^2)-(x/2) %the y funxtion.
    Instead of using * I used .*. Is this the correct method to multiply in matlab? Previously with numbers, I've used *, but I guess it's different with vectors and matrices. Also, I'm aware of the linspace "function" to get the graph smoother :)
     
    Last edited: Mar 16, 2014
  12. Mar 16, 2014 #11

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    This way, you just get a table with a few (5) function values.
    There are examples how to plot functions, you can take and modify them.
     
  13. Mar 17, 2014 #12
    Yes, I've done that now. This is my code for the graph:


    Code (Text):
    x = linspace(-2,2);

    y = atan(x) .* exp(1).^(-x.^2)-(x/2);

    plot(x,y)
    And I get a nice looking graph.

    Now I'm supposed to find the approximation of the first zero crossing (zero point?) of the function. I should do this by checking when the value of the function is less than 0. This example is attached to the problem:

    Code (Text):
    % zerocrossing.m
    x = -2:0.1:3;
    y = -0.3*x.^3+x+1;
    plot(x,y)
    %We can see from the graph that the value of the function is positive before the zero-crossing, and negative after
    for n = 1:length(y)
     if (y(n)<0)
     disp('y has changed (+/-) sign at x equal')
     disp(x(n))
     end
    end
     
    I'm not too sure what this code means. I'm assuming that this example contains different functions and "function area" (where I have -2<=x<=2), based on the two first lines. For my problem, should the two first lines in the above code look like this?

    Code (Text):
    x = -2:0.1:2;
    y = atan(x) .* exp(1).^(-x.^2)-(x/2);
    I'm not familiar with language used, I guess you could say. For instance, what does the below line literally mean?

    Code (Text):
    n = 1:length(y)
    I will continue to try and look into this myself obviously, but I've had a hard time the past few hours. Trial and error might help me heh.

    Appreciate any input, and thanks for all the help so far.
     
  14. Mar 18, 2014 #13
    Bump.

    I'm still lost on how to find the first zero crossing.

    Any input what so ever will be greatly appreciated.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: MatLab: About the basic functions about the program.
Loading...