As a hw problem i was asked to write a MATLAB program that uses cartesian coordi
ID: 2995390 • Letter: A
Question
As a hw problem i was asked to write a MATLAB program that uses cartesian coordinates to find the the location of a point relative to an origing in terms of polar coordinates(r and theta). I need to output a chart that displays the 'r' and theta for several sets of x and y. I am having trouble making it output for more than one set of x and y. here is my program! any help is really appreciated!
function [radius,angle] = polar_coordinates(x,y)
radius=(x.^2+y.^2);
angle=((atan(y/x)*(180/pi)));
if x<0 && y>0
angle=atan(y/x)*(180/pi)+180;
elseif x<0 && y<0
angle=atan(y/x)*(180/pi)-180;
elseif x<0 && y==0
angle=180;
elseif x==0 && y>0
angle=180/2;
elseif x==0 && y<0
angle=-180/2;
elseif x==0 && y==0
angle=0;
end
z=[x;y;radius;angle];
fprintf ('x y radius angle ');
fprintf('%d %3d %7.2f %9.2f ',z);
end
Explanation / Answer
clc
x=[2,2,0,-3,-2,-1,0,0,2];
y=[0,1,3,1,0,-2,0,-2,2];
radius = sqrt(x.^2+y.^2);
n = length(x);
angle = asin(y./radius)*(180/pi);
for i=1:n
if x(i)==0 && y(i)==0
angle(i) = 0;
elseif x(i)<=0 && y(i)>= 0
angle(i) = 180-angle(i);
elseif x(i)<=0 && y(i)<=0
angle(i) = -180-angle(i);
end
end
fprintf (' x y radius angle ');
for i=1:n
fprintf('%9.2f %9.2f %9.2f %9.2f ',x(i),y(i),radius(i),angle(i));
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.