Hi Ann,
Sounds like your function should take the following inputs: the first value of your sequence, the second value of your sequence, and the length of the sequence. The function should also return a vector "x" of iterative pair sums.
Be sure that this code is saved in a file name HW215_Ann.m. Then from the Matlab command window, you can type
to call your function (use any inputs you like, i used 1, 2, and 30). The output "x" should then be in your workspace with the sequence you wanted.
Function below:
function x = HW215_Ann(f1,f2,n)
if ~isnumeric(f1)||~isscalar(f1),
error('First input "f1" must be numeric scalar.');
end
if ~isnumeric(f2)||~isscalar(f2),
error('Second input "f3" must be numeric scalar.');
end
if ~isnumeric(n)||~isscalar(n),
error('Third input "n" must be numeric scalar.');
elseif (n~=round(n))||n<3,
error('Third input "n" must be an integer 3 or greater.')
end
x=NaN(n,1);
x(1)=f1;
x(2)=f2;
for iter=3:n
x(iter)=x(iter-1)+x(iter-2);
end
Functions in Matlab must be saved as .m files with the same name as the as the function's name. Also, you won't need to use return statements in Matlab. The objects you want to return should only be stated in the function declaration. Give this link a read-through.
Best Answer