Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please please help Matlab 1. Prepare a function m-file containing a function tha

ID: 3763098 • Letter: P

Question



Please please help
Matlab

1. Prepare a function m-file containing a function that converts polar coordinates in two- dimensional space to rectangular (Cartesian) coordinates. Include a suitable Hl 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) wll equal the number of points whose conversion from polar to rectangular coordinates is required. 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, 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

function [rect_x,rect_y] = polor2Rect(input_vec1,input_vec2)
% This function is the conversion of Polar Form into Rectangular Form
% It contains 2 inputs, input_vec1 for radius and input_vec2 for angle
% It contains 2 outputs, rect_x for x Coordinate and rect_y for y
% Coordinate
    rect_x=zeros(size(input_vec1,1),1);   
    rect_y=zeros(size(input_vec1,1),1);
    for i=1:size(input_vec1,1)
        r=input_vec1(i);
        theta=input_vec2(i);
        rect_x(i)=r.*cos(theta);    % X coordinate is rcos(theta)
        rect_y(i)=r.*sin(theta);    % Y coordinate is rsin(theta)
    end
end

% This Script is the test script for Polar to Rectangular Conversion
clc;                            % Clear Screen
clear all;                      % Clean previous variables
total_result=15;                 % No of results Are 15
radius=rand(total_result,1)*10; % Random radius generation between 0 to 10
angle=rand(total_result,1)*360; % Angle Theta in Degree between 0 to 360
result(:,1)=radius;           
result(:,2)=angle;
angle_radian=(angle.*pi)./180;    % Conversion of degree into radian
[result(:,3),result(:,4)]=polor2Rect(result(:,1),angle_radian);
%%% Print Formatting in Tabular Form
fprintf('Radius Angle X Axis Y Axis | Rectangular ');
disp('-------------------------------------------------------------------');
for i=1:size(result,1)
    fprintf('|%.2f %.2f %.2f %.2f | (%.2f + %.2f i) ',result(i,1),result(i,2),result(i,3),result(i,4),result(i,3),result(i,4));
end
disp('-------------------------------------------------------------------');