Question

In: Electrical Engineering

Objective: To provide practice (or a refresher) with writing simple MATLAB codes that employ common tools...

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

Solutions

Expert Solution

The value of radius should always be positive for all theta

Hence r(theta)=absolute(theta)

matlab code & result
required function code

function [x y]=poltocart(theta)
%funtion declaration with input theta of polar coordinate system
%and output x and y in cartesian coordinate system .it takes scalar values also
%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 theta value in given range-3*pi/2<=theta<=3*pi/2 -4
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 -3
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 -2
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 -1
Enter theta tvalue in given range-3*pi/2<=theta<=3*pi/2 0
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 1
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 2
Enter theta value in given range-3*pi/2<=theta<=3*pi/2 3
Enter theta value 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
outside of given range is theta

part 2 matlab code

%part 2 main program which uses function poltocart function for polar to
%cartesian conversion having 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 array of theta in given range-3*pi/2<=theta<=3*pi/2 [-2:0.1:2


Related Solutions

Objective: To provide practice (or a refresher) with writing simple MATLAB codes that employ common tools...
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...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods to advance a ship in two dimensional space. Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class that models a ship flying around in a 2-D plain (containing an x and y coordinate). A game class is provided to you that will help you call methods to advance and...
EXERCISE 8: IMPROVEMENT CASE STUDY Objective To practice quality improvement tools by applying them to an...
EXERCISE 8: IMPROVEMENT CASE STUDY Objective To practice quality improvement tools by applying them to an improvement effort in an ambulatory care setting. Instructions 1. Read the following case study. 2. Follow the instructions at the end of the case. Case Study Background You have just been brought in to manage a portfolio of several specialty clinics in a large multi-physician group practice in an academic medical center. The clinics reside in a multi-clinic facility that houses primary care and...
accounting research and practice assignment: 1-APA and MLA are the two common styles of writing your...
accounting research and practice assignment: 1-APA and MLA are the two common styles of writing your research work. Explore from website the two methods and explain why it is important to maintain these formats? 2-What is a Scholarly Source? thats all thank you
Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning....
Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning. Use 'whatis' or 'man' command to find out about each command. Your document should include the description or screen shots of the output from each of the command. Commands: df du gzip file history wget Changing access rights: chmod u+x Dir1.0             adds execute permission for the owner chmod go-w file1               removes write permission for the group and others chmod ugo=rw testfile      sets...
Financial ratios, along with common-size and trend statements, provide analysts with powerful tools for tracking a...
Financial ratios, along with common-size and trend statements, provide analysts with powerful tools for tracking a company's performance over time, for making comparisons among different companies and within the industry, and for assessing compliance with various benchmarks. Discuss what some of the limitations to the use of these tools might be? For example, what do you need to consider when evaluating the results and using them for decision-making purposes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT