Using Matlab I need help. \" Prepare a function m-file containing a function tha
ID: 3730544 • Letter: U
Question
Using Matlab I need help.
" Prepare a function m-file containing a function that converts polar coordinates in twodimensional space to rectangular (Cartesian) coordinates. Include a suitable H1 line and some additional comment lines. The input will be 2 vectors, and the output will be 2 vectors. The length of each vector (on input and on output) will equal the number of points whose conversion from polar to rectangular coordinates is required.(10pts) "
Instructions...
The radius of a point will appear as an element in the first input vector, and the angle (in radians) of that point will appear as the corresponding element in the second input vector. The first output vector will contain the horizontal (x) coordinates, and the second output vector will contain the vertical (y) coordinates. In the first section of your script m-file (the file that you will publish), generate 2 input vectors that you will use to test your function. Use at least 5 points, with any values you want. (Each input vector should be of length 5 or greater.) Then call your function to do the coordinate conversions. Finally, print a table. Each row in the table will correspond to one point. The first 2 columns (of 4 columns in the table) should contain the original polar coordinates, and the last 2 columns should contain the equivalent rectangular coordinates. Use formatted printing. The decimal points in each column should all lie on one vertical line.
Explanation / Answer
%r = 5 + 10*rand(6,1); - random assign
r = [5; 6; 7; 8; 9; 10];
the = [0; pi/2; pi/3; pi/4; pi/5; pi/6];
[x, y] = conv2cart(r, the);
tab = [];
for i = 1:size(x)
tab = [tab; r(i) the(i) x(i) y(i)] ;%make table and print
end
function [x, y] = conv2cart(r, theta)
%CONV2CART converts given vectors of polar into cartesian coordinates
%r should have the raidius while thet should have the angle(in radians)
%x and y will have the cartesian coordinates in the same order
x = r;
y = r;%Change x and y to same dimensions
sz = size(r);
for i = 1:sz
x(i) = r(i) * cos(theta(i));
y(i) = r(i) * sin(theta(i));
end
end
Sample Output -
>> Untitled
5.0000 0 5.0000 0
6.0000 1.5708 0.0000 6.0000
7.0000 1.0472 3.5000 6.0622
8.0000 0.7854 5.6569 5.6569
9.0000 0.6283 7.2812 5.2901
10.0000 0.5236 8.6603 5.0000
>>
Hope this helps :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.