MatLab: About the basic functions about the program.

AI Thread Summary
The discussion revolves around a beginner's assignment in MATLAB involving basic functions and transformations of a triangle defined by points A(1,3), B(-2,0), and C(2,-4). The user seeks guidance on rotating the triangle by 30 degrees, mirroring it around the x-axis, and scaling it, while also learning how to plot multiple triangles in the same coordinate system with different colors. They successfully implement the rotation and mirroring but encounter issues with plotting and understanding matrix operations in MATLAB. The conversation highlights the importance of using the correct multiplication methods for matrices and vectors, and the user expresses a desire to learn more about finding zero crossings in functions. Overall, the thread illustrates the challenges faced by beginners in mastering MATLAB's functionalities.
Mutaja
Messages
239
Reaction score
0
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.

Homework Statement



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:
% 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.

Homework Equations



Matrices. Rotation, scaling and mirror matrices.

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:
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.
 
Physics news on Phys.org
rotate the triangle 30 degrees in negative direction around origo
Around the origin.

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?
Yes. Just use the multiplication sign for matrix (and matrix&vector) multiplications.
 
mfb said:
Around the origin.

Yes. Just use the multiplication sign for matrix (and matrix&vector) multiplications.

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:
% 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:
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.
 
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.
 
mfb said:
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.

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:
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.
 

Attachments

  • Rotated.PNG
    Rotated.PNG
    2.8 KB · Views: 465
Last edited:
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.
 
mfb said:
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.

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.
 

Attachments

  • Rotated and mirrored.PNG
    Rotated and mirrored.PNG
    3.8 KB · Views: 537
Cyan is the blue triangle, mirrored at the x-axis.
Red is the blue triangle, rotated by -30 degrees.

Looks correct.
 
mfb said:
Cyan is the blue triangle, mirrored at the x-axis.
Red is the blue triangle, rotated by -30 degrees.

Looks correct.

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.
 
  • #10
For my second problem, I'm supposed to plot the graph of the function f(x) = arctan(x) * e-x^2 - \frac{x}{2} where -2<=x<=2.

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

Code:
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:
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:
  • #11
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.
 
  • #12
Yes, I've done that now. This is my code for the graph:


Code:
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:
% 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:
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:
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.
 
  • #13
Bump.

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

Any input what so ever will be greatly appreciated.
 

Similar threads

Back
Top