CS 122L - 5 Points Total
Objectives
● Learn how to use anonymous functions
● Practice with looping intervals
Deliverables
● Submit your pre-lab answers in Bblearn under the Lab 10 pre-lab assignment area.
1) The anonymous function allows us to easily create mathematical functions, and store them in variables. The documentation below provides additional details. http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html:
For example, let’s say we wanted to implement the equation x^2 +2*x +1. We could use an anonymous function to create a new function and store in in f:
f = @(x) x^2 + 2*x + 1;
Now, we can say f(3), and it will evaluate the function at 3 (plugging in 3 for all x).
a) Write the MATLAB code to create the function x^2 + 1, and evaluate that function at x = 5. …show more content…
( f ( 5 ) )
the output of this function is: ans =
26
b) Write the MATLAB code to loop from 1 to 5, and evaluate the function from part a at each iteration (the first iteration will be f(1), second f(2), etc.). You can use the looping variable as an argument to be passed to f.
The MATLAB code to loop from 1 to 5 and evaluate the function from part a at each iteration is:
f=@ ( x ) ( x ^ 2 + 1 ) ;
for i = [ 1 : 5 ]
f ( i )