14:440:127
Introduction to Computers for Engineers
Recitation 8- Problems for students to attempt
Please complete these problems on your own time if you do not finish in recitation. You don’t need to show us the
completed recitation problems, although we recommending saving a copy of your solutions for your own benefit.
1.
Create an anonymous function f = x + 5. Create another anonymous function
g = 2*x.^2. Now create h = g(f(x)) and evaluate its value at x=2.
f = @(x) x+5;
g = @(x) 2*x.^2;
h = @(x) g(f(x));
h(2)
2.
Create a function which takes a matrix of arbitrary size and returns a vector containing the minimum, mean and
maximum element in it. Then test it, giving an arbitrary matrix as input.
Save the following in threethings.m:
function v = threethings(m)
v(1) = min(min(m));
v(2) = mean(mean(m));
v(3) = max(max(m));
3.
Write your own sign function 'mysign(x)'. Given any numbers x, mysign(x) should return +1 if x > 0; 0 if x = 0; -
1 if x < 0.
Save the following in threethings.m:
function a = mysign(x)
if(x>0)
a = 1;
elseif(x<0)
a = -1;
else
a = 0;
end
Techically, sign should work for vectors and matrices too, so we could do:
function a = mysign(x)
a = x>0 + -1*x<0;
4.
Write a user-defined function 'sumsquare(N)'to calculate
1^2 + 2^2 + ... + (N-1)^2 + N^2.
For example, sumsquare(4) = 30.
Save the following as sumsquare.m
function out = sumsquare(N)
out = 0;
for z = 1:N
out = out + z^2;
end
5.
Write a Matlab function that returns the area and circumference of a circle for the given radius ‘
r
’.
If the user
requests a single output, return a 2 element vector with both the area and circumference.
If the user requests two
outputs, return the area and circumference separately.
(use nargout)
Save the following as circle.m
function [a b] = circle(r)
if(nargout==2)

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*