MatLab: About the basic functions about the program.

Click For Summary

Discussion Overview

The discussion revolves around using MATLAB for a homework assignment involving basic functions, specifically focusing on geometric transformations of a triangle and plotting functions. Participants share their experiences, code snippets, and challenges encountered while learning MATLAB.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant describes the assignment involving a triangle and outlines tasks including rotation, mirroring, and scaling using matrices.
  • Participants discuss the correct method for applying matrix transformations to the triangle's coordinates, with questions about the multiplication process in MATLAB.
  • There are inquiries about how to plot multiple triangles in the same coordinate system and how to change colors for different transformations.
  • One participant expresses confusion about the visibility of original triangle points in their plot and suspects a typo in the coordinates.
  • Another participant shares their approach to plotting a function and encounters an error related to matrix dimensions, leading to a discussion about element-wise operations in MATLAB.
  • Participants clarify the use of the 'hold on' command to overlay multiple plots and confirm the correctness of their plotted triangles.
  • There is mention of using the 'linspace' function for smoother graph plotting and an exploration of finding zero crossings of a function.

Areas of Agreement / Disagreement

Participants generally agree on the methods for plotting and transforming the triangle, but there are some uncertainties regarding specific details, such as the correct rotation direction and the visibility of points in the plot. The discussion about the function plotting also reveals differing levels of understanding about MATLAB operations.

Contextual Notes

Some participants express limitations in their understanding of MATLAB commands and functions, relying on trial and error or external resources for guidance. There are unresolved issues regarding the correct application of transformations and plotting techniques.

Who May Find This Useful

This discussion may be useful for students learning MATLAB, particularly those working on geometric transformations and function plotting in a homework context.

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: 490
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: 562
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

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K