EG 10112:
Introduction to Engineering
Homework 3
Due in Learning Center, week of February 13, 2012
Reading: Brockman 12.3-12.5 (no user-defined functions, yet)
1.
If a program contains the following line, for what values of
iv
will the loop run?
for iv=3:4:17
The loop will run for the values of iv = 3, 7, 11, 15, and then end (it starts at 3, increments by 4, and
stops when it reaches 17).
2.
List all possible values for
num
, below, in which the following program will display “None of the
above.”
num=randi([1,100]);
if
num>0 && num<5
x=1;
elseif
num>10 && num<70
x=2;
elseif
num>85
x=3;
else
x=
'None of the above'
;
end
disp(x)
The program will display
“
None of the Above
”
when n=5-10 (including the values of 5 and 10) or
when n=70-85 (including the values of 70 and 85).
All other values from 1 to 100 would fit in the
confines of the problem.
3.
piecewise.m
Write a program,
piecewise.m
, that calculates the value of the piecewise function
p(x)
defined
below for an input
x
. The function is defined in three parts:
otherwise
defined
not
10
4
if
5
2
4
1
if
3
1
1
5
if
4
2
)
(
2
x
x
x
x
x
x
x
p
The program should get the value of the argument
x
from the user with an
input
command. The
display both the input value and function value in a nicely formatted way.

% User indicates an input and the then result will be displayed
% Clear command window and workspace
clear
clc
x = input(
'Please enter a value: '
);
% Create a logical variable to change between true and false if user
% chooses an undefined x.
notDefined = false;
% Determin p
% Determine the function values using a if/elseif/else structure
if
x>=-5 && x<-1
p=2*x-4;
elseif
x>=-1 && x<4
p=(x-1/3)^2;
elseif
x>=4 && x<10
p=-2*x+5;
else
notDefined=true;
end
% Display results
disp([
'For the input value x = '
num2str(x),
','
]);
if
notDefined
disp(
'p(x) is not defined.'
);
else
disp([
'p(x) = '
, num2str(p),
'.'
]);
end
OUTPUTS:
Please enter a value: 7
For the input value x = 7,
p(x) = -9.

#### You've reached the end of your free preview.

Want to read all 7 pages?

- Fall '14
- matlab, Randomness, disp