Exercises 3 (to be completed using MATLAB) 1. Write a MATLAB function that takes
ID: 3767295 • Letter: E
Question
Exercises 3 (to be completed using MATLAB)
1. Write a MATLAB function that takes as its input arguments the radius and length of the cylinder.
The function should return the surface area and volume of the cylinder as output argument.
2. Write a MATLAB function that takes as its input argument a vector and returns the average and the sum of the elements in the vector.
3. Write a MATLAB script call oppSign.m that asks the user for a number. If the number is positive is should print ‘The number is positive’ to the screen. If it is negative, it should print out ‘The number is negative’. If it is neither, it should print out something stating that.
4. Write a MATLAB function that takes two integers, k and l, as its input arguments and returns the sum of the squares of all integers between k and l (inclusive of k and l). The function must use a for loop.
5. Consider a nuclear reactor whose temperature is monitored by three sensors. An alarm should go on if any two of the sensor readings disagree. Write a function called nukeAlarm.m that takes the three sensor reading as input arguments and outputs the string 'alarm!' if any two of the temperature readings disagree by more than 10 degrees and 'normal' otherwise. (Hint, you will find the built-in abs function useful.)
Explanation / Answer
1.
function [ surfaceArea, volume ] = Cylinder( radius, length )
%A MATLAB function that takes as its input arguments the radius and length of the cylinder.
%The function should return the surface area and volume of the cylinder as output argument.
surfaceArea = 2 * pi * radius * (length + radius)
volume = pi * radius * radius * length
end
2.
function [ summation, average ] = SumAndAverage( A )
%A MATLAB function that takes as its input argument a vector and returns the average and the sum of the elements in the vector.
summation = sum(A)
average = mean(A)
end
3.
number = input('Enter the number: ');
if(number < 0)
display 'The number is negative.';
elseif(number > 0)
display 'The number is positive.';
else
display 'The number is neither negative nor positive.';
end
4.
function [ sum ] = SumOfNumbersBetween( k, l )
%A MATLAB function that takes two integers, k and l, as its input arguments
%and returns the sum of the squares of all integers between k and l (inclusive of k and l).
%The function must use a for loop.
sum = 0;
if(k < l)
for i = k:l
sum = sum + i*i;
end
else
for i = l:k
sum = sum + i*i;
end
end
5.
function nukeAlarm( sensor1, sensor2, sensor3 )
%a function called nukeAlarm.m that takes the three sensor reading as input arguments and outputs the string 'alarm!'
%if any two of the temperature readings disagree by more than 10 degrees and 'normal' otherwise.
X = sensor1 - sensor2;
Y = sensor2 - sensor3;
Z = sensor3 - sensor1;
if abs(X) > 10 || abs(Y) > 10 || abs(Z) > 10
display 'alarm';
else
display 'normal';
end
end
If you still have further queries, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.