You are on page 1of 4

FE211

Fall 2020
Lab Session-6 Solutions

1. a) The following set of commands on the command window can be used:

>> Cp=1; m=1


>> heat = @(deltaT) m*Cp*deltaT;
>> cal_to_J = @(cal) cal*4.2;
>> cal_to_J(heat(20))
ans = 84

b) The following set of commands on the command window can be used:

>> fplot(heat,[0 100])


>> xlabel('deltaT (K or C)'), ylabel('E (cal)')
>> title('E vs. deltaT')

Note that the limits for ∆T are given in terms of ⁰C. Our function ‘heat’ calculates the
energy (E) required to for a change of ∆T (in K) in water at constant pressure. However,
as ⁰C+273.15=K , ∆T(⁰C) = ∆T(K). So, we can directly use the given ∆T limits to plot the
function without any need for unit conversion.

2. Solution:
%a)
>> f=@(z) (1/sqrt(2*pi))*exp(-z.^2/2);
>> fplot(f,[-4,4])
>> xlabel('z'),ylabel('f(z)')
>>%b)
>> x=@(t) sin(t).*(exp(cos(t))-2*cos(4*t)-sin(t/12).^5);
>> y=@(t) cos(t).*(exp(cos(t))-2*cos(4*t)-sin(t/12).^5);
>> fplot(x,[0,100])
>> hold on
>> fplot(y,[0,100],':c')
>> xlabel('t'),ylabel('x ot y')
>> legend('x','y')
3. a)An example function M-file can be as follows:

function Ta=avgtempi(Tm,Tp)
w=2*pi/365;
ts=input('Starting day: ');
te=input('Ending day: ');
t=ts:te; % ts: starting day, te: ending day
T=Tm+(Tp-Tm)*cos(w*(t-205)); % temperature of a specific day,t.
Ta=mean(T) % mean temperature averaged for all the days
% calculated in the range from ts to te.
endfunction

b) Running the program for January-March in Artvin (t = 0 to 90)


gives:

>> avgtempi(17,30);
Starting day: 0
Ending day: 90
Ta = 6.1564

Running the program for July-August in İzmir (t = 181 to 243) gives:

>> avgtempi(23.4,42);
Starting day: 181
Ending day: 243
Ta = 40.974

4. An example function M-file can be as follows:

function currency_converter2(a,b)
%This function converts between dollars to euros,liras and yens
%On Monday, 28 December, 2020 :
%1 US Dollar=103.87 Japanese Yen
%1 US Dollar=7.43 Turkish Liras
%1 US Dollar=0.82 Euros

%Generate a vector for dollars between values a and b


dollars = a:b;
%Call all the subfunctions to make the conversions and assign each
%result to a separate variable
euros = dollar_to_euro(dollars);
liras = dollar_to_lira(dollars);
yens = dollar_to_yen(dollars);
%Generate the table of conversions
table = [dollars;euros;liras;yens]; %We don't transpose it because
it
%will be used with fprint.
%Give a title to your table
disp(' ')
disp('Conversion table- $ to various currencies')
%Give column titles
disp('dollars euros liras yens')
%Print the table in the desired format
fprintf('%4d\t%8.3f\t%8.3f\t%8.3f \n',table)
%End the primary function
endfunction

%Start the subfunction dollar_to_euro


function [euros]= dollar_to_euro(dollars)
%Make the conversion from dollars to euros
euros = dollars*0.82;
%End the subfunction
endfunction

%Start the subfunction dollar_to_lira


function [liras]= dollar_to_lira(dollars)
%Make the conversion from dollars to liras
liras = dollars*7.43;
%End the subfunction
endfunction

%Start the subfunction dollar_to_yen


function [yens]= dollar_to_yen(dollars)
%Make the conversion from dollars to yens
yens = dollars*103.87;
%End the subfunction
endfunction

Running the program from the command window gives;

>> currency_converter2(1,10)

Conversion table- $ to various currencies


dollars euros liras yens
1 0.820 7.430 103.870
2 1.640 14.860 207.740
3 2.460 22.290 311.610
4 3.280 29.720 415.480
5 4.100 37.150 519.350
6 4.920 44.580 623.220
7 5.740 52.010 727.090
8 6.560 59.440 830.960
9 7.380 66.870 934.830
10 8.200 74.300 1038.700

You might also like