MatLab: About the basic functions about the program.

In summary, the student is stuck on how to plot multiple triangles in the same coordinate system and is asking for help. The student has been using Google to try to figure out how to do this, but is having trouble. They have been successful in completing the first part of the homework by rotating the triangle around the origin. They are now stuck on how to plot the new triangles. They have been successful in completing the second part of the homework by mirroring the rotated triangle around the x-axis.
  • #1
Mutaja
239
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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: 428
Last edited:
  • #6
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.
 
  • #7
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: 492
  • #8
Cyan is the blue triangle, mirrored at the x-axis.
Red is the blue triangle, rotated by -30 degrees.

Looks correct.
 
  • #9
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 - [itex]\frac{x}{2}[/itex] 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.
 

1. What is Matlab and what are its basic functions?

Matlab is a high-level programming language and interactive environment used for numerical computation, data analysis, and visualization. Some of its basic functions include performing mathematical operations, creating and manipulating arrays, and plotting data.

2. How do I create and manipulate arrays in Matlab?

To create an array in Matlab, you can use the "zeros" or "ones" function to initialize an array of all zeros or all ones, respectively. You can also use the "linspace" function to create an evenly spaced array. To manipulate arrays, you can use functions such as "reshape" to change the dimensions, "transpose" to switch rows and columns, and "sort" to sort the elements.

3. What are some common mathematical operations that can be performed in Matlab?

Matlab has a wide range of built-in mathematical functions, including addition, subtraction, multiplication, division, exponentiation, and trigonometric functions. It also has functions for solving linear and nonlinear equations, finding roots, and performing matrix operations.

4. How can I plot data in Matlab?

Matlab has powerful plotting capabilities, allowing you to create 2D and 3D plots, histograms, scatter plots, and more. You can use the "plot" function to plot a line graph, "bar" function to create a bar graph, and "scatter" function to create a scatter plot. You can also customize the appearance of your plots by adding labels, legends, and titles.

5. Can I use Matlab for data analysis and visualization?

Yes, Matlab is commonly used for data analysis and visualization. It has functions for importing and exporting data, cleaning and manipulating data, and performing statistical analysis. It also has a variety of visualization tools, such as histograms, scatter plots, and heatmaps, to help you better understand your data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
824
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
4
Views
728
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Linear and Abstract Algebra
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top