Problem
A vector can
be represented by its rectangular coordinates x and
y or by its polar coordinates r
and y. The relationship between them
is given by the equations:
x = r*cos(θ)
y =
r*sin(θ)
Solution
This problem has been
taken from the book Practical Introduction to Matlab by Stormy Attaway. This is
a very good book to read for people who are new to Matlab and also if you want
to refresh yourself the concepts of Matlab, then this is a great book. This
book assumes that you are new to Matlab and will teach you from beginning and
make you a professional in Matlab programming. I suggest you read this book
once if you really want to become a pro in Matlab.
You can learn about
coordinate systems from here: Wikipedia - Coordinate system. If you know about the polarcoordinate system and Cartesian coordinate system and have learnt about it a
long back then find the image below to brush yourself about the coordinate
systems.
Image Credits: mathinsight.org |
This problem is very
simple. Before we solve this problem on Matlab type the following command:
help elfun
You can find that there
are many elementary functions present in Matlab and some of them are sin, cos,
tan, sind, cosd, tand and so on. I have mentioned only a few of them because
they are important for us now, while remaining are also important but not
necessary for solving this problem. I recommend you to have a glance at all the
functions so that they will be useful for you in the future. Okay lets know
what is the difference between sin and sind and in the similar way cos and
cosd, tan and tand and so on. To be simple the difference is that sin assumes
that the input parameter is in radians and on the other side sind assumes that
the input is in degrees (‘d’ in sind
represents degrees). So here let us assume that the user is giving input in
radians as most of the mathematical problems corresponding to polar coordinates
are in radians. You can also refer to the official documentation in the links
below.
Coming back to problem, it
is very simple because we are directly converting the equations given in the
question to Matlab code. Have a look at the program first and if you still
don’t understand then refer to the explanation section below the program section
and I am sure you will understand the solution perfectly. Some of the concepts
used in the program to solve the above problem are as follows:
1) Request User Input - Input Function
2) cos
3) sin
4) fprintf function
5) Elementary functions in Matlab
1) Request User Input - Input Function
2) cos
3) sin
4) fprintf function
5) Elementary functions in Matlab
Program
%This program is for converting the polar co ordinates
%To cartesian co ordinates
%User input for the angle
angle = input('Enter the Angle of the polar coordinates: ');
%User input for r
r = input('Enter the value of "r": ');
%Convert according to equations given
x = r*cos(angle);
y = r*sin(angle);
%Display the same to the user
fprintf('x = %.2f\n',x)
fprintf('y = %.2f\n',y)
I have named my file as polarcartesian.m and have saved it on my local computer.
Explanation
We are using the input function in Matlab to get the value of
angle in radians(See the solution section for why we are getting the value in
radians). Then we are getting the value of ‘r’ using the input function. We are
storing the values of angle in variable angle and value of ‘r’ in radians. We
are using the elementary functions cos and sin to write the equations given in
the question. Finally we are using fprintf function to display the value of x
and y to the user.
Input/Output
Matlab polarcartesian - Convert polar coordinates to rectangular coordinates |
Test Yourself
Try to write a program to convert from Cartesian coordinate system
to polar coordinate system and send it to me or comment below.
If you still have any
doubt even after reading the complete post or if you have found any mistake
then please do comment below or contact me so that it will not only help you
but also the community. You can contact me from here: Contact me
Comment the value of x and y you are
getting for a given input in the comment box below.
Program in this post has been high lighted using hilite.me
Keywords:
Polar to Cartesian, rectangular coordinates, polar coordinates,