In: Electrical Engineering
Objective: To provide practice (or a refresher) with writing simple MATLAB codes that employ common tools such as loops, functions, arrays, and MATLAB plotting tools.
P T 1: Functions and loops
The goal of this exercise is to plot the function
r(theta) = theta
where theta is a phase angle and r(theta) is the radius at that
angle, over the range - 3pi/2 =< theta =< 3pi/2 .
It is often useful to define a function to handle calculations that will be performed over and over again. Designing your code this way presents several advantages:
•Individual functions that can build specific tasks can be built and tested separately from the rest of the code. this not only can make your program easy to debug, it breaks the programming tasks into smaller more manageable chunks that can potentially be shared among multiple coders.
•Functions can be used by more than one program - so if you've written a function that does something useful, you can reuse it if you need to perform the same task in another program.
We will write a simple program that plots the above function using a main program and a nction. In teams of two, decide which person will take the lead on the nction and which person will take the lead on the main program.
The FUNCTION should take as input the phase angle theta and return as output the Cartesian (x,y) coordinates corresponding to the point (theta,r(theta)) (defined as above). Recall that the following functions can be used to convert polar coordinates to Cartesian coordinates:
X = rcos(theta) y = rsin(theta)
For help with the syntax r nctions in MATLAB, type
help function
on the command line in MATLAB.
The MAIN PROGRAM should use a loop to pass phase angles one-at-a-time to the function over the range of inputs, collect the outputs, and then plot the results. For help with the syntax of loops in MATLAB (and to help consider what kind of loop would be appropriate here), use the MATLAB help to and out more about the following:
for if while
Part 2:
Passing arrays to functions
Repeat the above exercise,but instead of using a loop to pass individual values of theta to the function,the main program should define a vector of all values of theta that should be used, and pass the whole vector to the function. What needs to be changed in the function, if anything, to accommodate this change? Once these changes are made, will the function still work when scalar values of are passed to it? (If not, try to make the necessary changes so that the function will work for both scalar and vector inputs.)
Part 3: Stopping conditions
Repeat the exercise, but instead of using a pre-determined range for theta, keep going until y >= 10 . How does this affect (if at all) the type of loop you need to use?
redius should be positive for all theta so r(theta)=absolute(theta)
matlab code and result
code for required function
function [x y]=poltocart(theta)
%funtion declaration with input theta of polar coordinate
system
%and output x and y in cartesian coordinate system it accept scalar
as well
%as array of theta input
rtheta=abs(theta);
x=rtheta.*cos(theta);
y=rtheta.*sin(theta);
part 1 matlab code
%part 1 main program which uses function poltocart function for
polar
%to cartesian conversion and loop to get theta value one at a time
with
%pre-determined range of theta
clear all;clc;
k=0;
while (1)
k=k+1;
theta=input('Enter the value of theta in given
range-3*pi/2<=theta<=3*pi/2 ');
if((-3*pi/2<=theta)&&(theta<=3*pi/2))
[x(k) y(k)]=poltocart(theta);
else
disp('theta is outside of given range');
break;
end
end
plot(x,y);
title('plot of r(theta) function in cartesian coordinate');
xlabel('X');ylabel('Y')
part 1 result
Enter the value of theta in given
range-3*pi/2<=theta<=3*pi/2 -4
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
-3
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
-2
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
-1
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
0
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
1
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
2
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
3
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
4
Enter the value of theta in given range-3*pi/2<=theta<=3*pi/2
5
theta is outside of given range
part 2 matlab code
%part 2 main program which uses function poltocart function for
polar to
%cartesian conversion with array input of theta
clear all;clc;
theta=input('Enter the array of theta in given
range-3*pi/2<=theta<=3*pi/2 ');
[x y]=poltocart(theta);
plot(x,y);
title('plot of r(theta) function in cartesian coordinate');
xlabel('X');ylabel('Y')
part 2 result
Enter the array of theta in given range-3*pi/2<=theta<=3*pi/2 [-2:0.1:2]
part 3 matlab code
%part 3 main program which uses function poltocart function
for
%polar to cartesian conversion and loop to get theta value one at a
time
%but check y>=10
clear all;clc;
k=0;
while (1)
k=k+1;
theta=input('Enter the value of theta ');
[x(k) y(k)]=poltocart(theta);
if (y(k)<10)
disp('Y is outside of given range');
break;
end
end
plot(x,y);
title('plot of r(theta) function in cartesian coordinate');
xlabel('X');ylabel('Y')
part 3 result
Enter the value of theta 20
Enter the value of theta 21
Enter the value of theta 40
Enter the value of theta 45
Enter the value of theta 2
Y is outside of given range