In polar coordinates a two-dimensional vector is given by its radius and angle (
ID: 3796788 • Letter: I
Question
In polar coordinates a two-dimensional vector is given by its radius and angle (r, theta). Write a user- defined MATLAB function that adds two vectors that are given in polar coordinates. For the function name and arguments use [r th] = AddVecpol(r1, th1, r2, th2), where the input arguments are (r_1, theta_1) and (r_2, theta_2), and the output arguments are the radius and angle of the result. Use the function to carry out the following additions: r_1 = (5, 23degree), r_2 = (12, 40degree) r_1 = (6, 80degree), r_2 = (15, 125degree) The fuel efficiency of an automobile is measured in mi/gal (miles per U.S. gallon) or in km/L (kilometers per liter). Write a MATLAB user-defined function that converts fuel efficiency values from km/L to mi/gal. For the function name and arguments use mpg= kmlTOmpg (kml). The input argument kml is the efficiency in km/L, and the output argument mpg is the efficiency in mi/gal. Use the function in the Command Window to: (a) Determine the fuel efficiency in mi/gal of a car that consumes 9 km/L. (b) Determine the fuel efficiency in mi/gal of a car that consumes 14 km/LExplanation / Answer
The Matlab function AddVecPol.m
function [r,th] = addvecPol(r1,th1,r2,th2)
r = sqrt(r1^2+r2^2+2*r1*r2*cosd(th2-th1)); % Computing r
th = th1 + atan2d(r2*sind(th2-th1),r1+r2*cosd(th2-th1)); % Computing theta
end
Testing the function
>> r1 = 5;th1 = 23;
>> r2 = 12;th2 = 40;
>> [r,th] = addvecPol(r1,th1,r2,th2)
r =
16.8451
th =
35.0215
>> r1 = 6;th1 = 80;
>> r2 = 15;th2 = 125;
>> [r,th] = addvecPol(r1,th1,r2,th2)
r =
19.7048
th =
112.5663
>>
Matlab function kmlTompg.m
function mpg = kmlTompg(kml)
mpg = kml*2.35215; % Converting km/l to mil/gal
end
Testing th efunction
>> mpg = kmlTompg(9)
mpg =
21.1694
>> mpg = kmlTompg(14)
mpg =
32.9301