>> x = [0: pi/100: pi]; % [start: increment: end] >> y = sin(x); >> plot(x,y), title('Simple Plot')
Intro MATLAB
Line color, style, marker type, Line color, style, marker type, all within single quotes; type all within single quotes; type >> doc LineSpec >> doc LineSpec for all available line properties for all available line properties
Intro MATLAB
ans = 100 >> u = myfun(2,3) % called with one output u = 100 >> [u v w] = myfun(2,3) % called with all outputs u = 100 v = 6 Any return value which is not stored Any return value which is not stored w = in an output variable is simply in an output variable is simply 4 discarded discarded
Intro MATLAB
Problem 1
Problem 1
Solution 1
function sincomp(x,n) i = 1; tru = sin(x); ser = 0; //Value from Series fprintf('\n'); fprintf('order true value approximation error\n'); while (1) if i > n, break, end ser = ser + (-1)^(i - 1) * x^(2*i-1) / factorial(2*i-1); er = (tru - ser) / tru * 100; fprintf ('%3d %14.10f %14.10f %12.8f\n', i, tru, ser, er); i = i + 1; end
Solution 1
>> sincomp (1.5,8) order true value approximation error 1 0.9974949866 1.5000000000 -50.37669564 2 0.9974949866 0.9375000000 6.01456523 3 0.9974949866 1.0007812500 -0.32945162 4 0.9974949866 0.9973911830 0.01040643 5 0.9974949866 0.9974971226 -0.00021414 6 0.9974949866 0.9974949557 0.00000310 7 0.9974949866 0.9974949869 -0.00000003 8 0.9974949866 0.9974949866 0.00000000
Problem 2
Problem 2
Problem 2
Solution 2
function [r, th] = polar(x, y) r = sqrt(x .^ 2 + y .^ 2); if x < 0 if y > 0 // second quadrant condition th = atan(y / x) + pi; elseif y < 0 // third quadrant condition th = atan(y / x) - pi; else th = pi; end else if y > 0 th = pi / 2; elseif y < 0 th = -pi / 2; else th = 0; end end th = th * 180 / pi;
Problem 3
Problem 3
Solution 3
function Manning(A) A(:,5) = sqrt(A(:,2))./A(:,1).*(A(:,3).*A(:,4)./ (A(:,3) +2*A(:,4))).^(2/3); fprintf('\n n S B H U\n'); fprintf('%8.3f %8.4f %10.2f %10.2f %10.4f\n',A');
Solution 3
A= 0.0350 0.0200 0.0150 0.0300 0.0220 0.0001 0.0002 0.0010 0.0007 0.0003 10.0000 2.0000 8.0000 1.0000 20.0000 1.5000 24.0000 3.0000 15.0000 2.5000
>> Manning(A) nSBH 0.035 0.020 0.015 0.030 0.022 >> U 0.0001 0.0002 0.0010 0.0007 0.0003 10.00 8.00 20.00 24.00 15.00 2.00 1.00 1.50 3.00 2.50 0.3624 0.6094 2.5167 1.5809 1.1971
Problem 4
Economic formulas are available to compute annual payments for loans. Suppose that you borrow an amount of money P and agree to repay it in n annual payments at an interest rate of i. The formula to compute the annual payment A is
Problem 4 (Contd..)
Write an M-file to compute A. Test it with P = Rs.1,00,000.00 and an interest rate of 6% (i = 0.06). Compute results for n = 1, 2, 3, 4, and 5 and display the results as a table with headings and columns for n and A.
Solution 4
function annualpayment(P, i, n) nn = 1:n; A = P*i*(1+i).^nn./((1+i).^nn-1); y = [nn;A]; fprintf('\n year annualpayment\n'); fprintf('%5d %14.2f\n',y);
The following pack all output pack all output >> [s,rows,cols] = values values mysize(rand(4,5)) into varargout cell into varargout cell array array returns s = [4 5], rows = 4, cols = 5
Intro MATLAB
function [root,fx,ea,iter]=bisection(func,xl,xu,es,maxit,varargi n) % bisect: root location zeroes % [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...): % uses bisection method to find the root of func % input: % func = name of function % xl, xu = lower and upper guesses % es = desired relative error (default = 0.00001%) % maxit = maximum allowable iterations (default = 50) % p1,p2,... = additional parameters used by func % output: % root = real root
if nargin<3,error('at least 3 input arguments required'),end test = func(xl,varargin{:})*func(xu,varargin{:}); cont=0; while (cont==0) if (test>0) s= input (' Enter new bound type l/u :', 's'); xv=input (' Enter value: '); if (s=='l') xl=xv; else xu=xv; end test = func(xl,varargin{:})*func(xu,varargin{:});