The unit closes with a few simple exercises for you to test the skills you have learned throughout the unit.
Problem 1: Hint based problem
max
or min
.
Your functions should take in a vector and return values for the maximum and minimum.The following if
statement will find the maximum between max_x
and x(i)
:
if x(i)>max_x
max_x=x(i);
end
doc function
for help on how to return multiple values.The following code will calculate the maximum of an array passed to it:
function [max_x] = maximum(x)
max_x=x(1);
for i=1:length(x)
if x(i)>max_x
max_x=x(i);
end
end
The following code will calculate the minimum of an array passed to it.
function [min_x] = minimum(x)
min_x=x(1);
for i=1:length(x)
if x(i)<min_x
min_x=x(i);
end
end
To use these, save the above a .m
files with the appropriate names, and run the commands:
minimum(x)
maximum(x)
where x
is a vector.
The following code will calculate the minimum and the maximum of an array passed to it:
function [min_x,max_x] = minmax(x)
min_x=x(1);
max_x=x(1);
for i=1:length(x)
if x(i)<min_x
min_x=x(i);
end
if x(i)>max_x
max_x=x(i);
end
end
To call the function, save the above as minmax.m
, and run:
[minimum,maximum] = minmax(x)
Problem 2: Hint based problem
$y=x^3$
and $y=x^5$
, on the interval $[-2,\,2]$
, on the same graph.
Where are the intersections of the curves?You may want to create the following vectors:
x=linspace(-2,2,100);
y1=x.^3;
and use the plot functionality you learned in this unit.
.png
file.Intersections occur at $(x,\,y)=(−1,\,−1),(0,\,0),(1,\,1)$
.
The following commands plot the curves:
x=linspace(-2,2,100);
y1=x.^3;
y2=x.^5;
plot(x,y1,'-',x,y2,'--');
The following commands will add a label and legend to the figure:
legend('y=x^3','y=x^5');
xlabel('x');
ylabel('y');
You can save the file by using ‘Save As’ in the ‘File’ menu or by using the following command.
print ExampleFigure.png -dpng