QUESTION 1) Write a code using MATLAB. a) Take a numerical input and an alphabet
ID: 2249009 • Letter: Q
Question
QUESTION 1)
Write a code using MATLAB.
a) Take a numerical input and an alphabet from the user,
then convert this numerical input into temperature in Celsius or in
Fahrenheit based on the alphabet. (i.e input [32, F]=> output [0, C]).
The result should be delivered to the main workspace.
b) Take three numbers
and an alphabet from the user,
then converting the number into Coordinate to other two Coordinate Systems.
(i.e input [##,##,##,C] => output [##,##,##,Y], [##,##,##,S]).
The result should be delivered in the main workspace.
Explanation / Answer
Ans)
=============part-a matlab code======
function [ temp2, str2 ] = temp_conv( temp1,str1 )
%TEMP_CONV Summary of this function goes here
% Detailed explanation goes here
if str1=='C' % if entered string is 'C'
temp2=((9/5)*temp1)+32; % convert to farenheit
str2='F';
end
if str1=='F' %if entered string is 'F'
temp2=(temp1-32)*5/9; % convert to celsius
str2='C';
end
end
====================
the function when run from command window
>> [temp str]=temp_conv(0,'C')
temp =
32
str =
F
>>
========================
===Part-b matlab code======
function [ cor1,cor2 ] = coordinate_conv( var1,var2,var3,str1 )
%COORDINATE_CONV Summary of this function goes here
% Detailed explanation goes here
if str1=='Y' %if given co-ordinates are cartesian
x=var1;y=var2;z=var3;
r=sqrt(x^2+y^2);
theta=atan(y/x);
cor1={r theta z 'C'}; %cyclindrical co-ordinates
p=sqrt(x^2+y^2+z^2);
theta=atan(y/x);
phi=atan(sqrt(x^2+y^2)/z);
cor2={p theta phi 'S'};%spherical co-ordinates
end
if str1=='C' %if given co-ordinates are cylindrical
r=var1;theta=var2;z=var3;
x=r*cos(theta);
y=r*sin(theta);
cor1={x y z 'Y'}; %cartesian co-ordinates
p=sqrt(r^2+z^2);
phi=atan(r/z);
cor2={p theta phi 'S'};%spherical co-ordinates
end
if str1=='S' %if given co-ordinates are spherical
p=var1;theta=var2;phi=var3;
x=p*cos(theta)*sin(phi);
y=p*sin(theta)*sin(phi);
z=p*cos(phi);
cor1={x y z 'Y'}; %cartesian co-ordinates
r=p*sin(phi);
z=p*cos(phi);
cor2={r theta z 'C'}; %cyclindrical co-ordinates
end
end
==============
results when run from command window
>> clear
>> [cord1 cord2]=coordinate_conv(3,4,5,'Y')
cord1 =
[5] [0.9273] [5] 'C'
cord2 =
[7.0711] [0.9273] [0.7854] 'S'
>>
=============================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.