02.03.2016 Views

MATLAB by rudra pratap

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.5 Lesson 5: Creating and Executing a Function File<br />

37<br />

4. function w = crossprod (u , v) ;<br />

% CROSSPROD : function to compute w u x v for vectors u & v<br />

% call syntax:<br />

% w = crossprod (u,v) ;<br />

% ------------<br />

if length (u) >3 I length (v) >3 % check if u OR v has > 3 element s<br />

end<br />

error ('Ask Euler . This cross product is beyond me .')<br />

w = [u (2) *v (3)-u(3) *v(2) ;<br />

u(3) *v(l)-u(l)*v(3) ;<br />

u(l)*v(2)-u(2)*v(1)] ;<br />

% first element of w<br />

% se cond element of w<br />

% third element of w<br />

Can you modify this function to check for 2-D vectors and set the third component<br />

automatically to zero?<br />

5. function s = gseriessum(r,n) ;<br />

% GSERIESSUM : function to calculate the sum of a geometric series<br />

% The series is 1+r+r-2+r-3+ . . . . r-n (up to nth power) .<br />

% call syntax :<br />

% s = gseriessum(r,n) ;<br />

% ------------<br />

nvector =<br />

O:n;<br />

series = r.-nvector ;<br />

s = sum ( s eries) ;<br />

% create a vector from 0 to n<br />

% create a vector of terms in the series<br />

% sum all elements of the vector 'series' .<br />

6. function [capital ,interest] = compound (capital ,years ,rate ,timescomp) ;<br />

% COMPOUND : function to compute the compounded capital and the interest<br />

% call syntax :<br />

% [capital ,interest] = compound (capital ,years ,rat e ,timescomp ) ;<br />

% ------------<br />

xO = capital ; n = years ; r = rate ; k = timescomp ;<br />

if r>l<br />

% check for common mistake<br />

end<br />

disp('check your interest rate . For 8% enter .08, not 8. ')<br />

capital = xO* (l+r/k) -(k*n) ;<br />

interest = capital - xO ;<br />

[Ans. (i) Quarterly: $346.86, Daily: $349.83, Difference: $2.97.]

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

Saved successfully!

Ooh no, something went wrong!