11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

function [A] = area2(a,b,c)% Compute the area of a triangle whose% sides have length a, b and c.% Inputs: either% a,b,c: Lengths of 3 sides% or% a, b: two shortest sides of a% right angled triangle% Output:% A: area of triangle% Usage:% Area = area2(2,3,4);% or% Area = area2(3,4);% Written by dfg, Oct 14, 1996.% Extended Oct 25, 2012if nargin > dice(3)ans =6 12 34 1>> sum(dice(100))/100ans =3.8500 3.4300The last command gives the average value over100 throws (it should theoretically have the value3.5).Example 22.3 Construct a function that willreturn the nth Fibonnaci number f n , wheref 1 =0,f 2 =1andf n = f n 1 + f n 2 , n =3, 4, 5,....(See Example 18.1.) The function has:• Input: Non-negative integer n• Output: f nWe shall describe four possible functions andtry to assess which provides the best solution.Method 1:File Fib1.mfunction f = Fib1(n)% Returns the nth number in the% Fibonacci sequence.F = zeros(1,n);F(2) = 1;for i = 3:nF(i) = F(i-1) + F(i-2);endf = F(n);35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!