Upload
devbrat-singh
View
219
Download
0
Embed Size (px)
Citation preview
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 1/22
Intro MATLAB
Easy 2-D Graphics
>> x = [0: pi/100: pi]; % [start: increment: end]
>> y = sin(x);
>> plot(x,y), title('Simple Plot')
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 2/22
Intro MATLAB
Adding Another Curve
Line color, style, marker type,all within single quotes; type
>> doc LineSpec
for all available line properties
Line color, style, marker type,
all within single quotes; type
>> doc LineSpec
for all available line properties
>> z = cos(x);
>> plot(x,y,'g.',x,z,'b-.'),title('More complicated')
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 3/22
Intro MATLAB
function [a b c] = myfun(x, y)
b = x * y; a = 100; c = x.^2;
>> myfun(2,3) % called with zero outputsans =
100>> u = myfun(2,3) % called with one outputu =
100>> [u v w] = myfun(2,3) % called with all outputs
u =100
v =
6w =
4
Functions – First Example
Write these two lines to a file
myfun.m and save it on MATLAB’s path
Write these two lines to a file
myfun.m and save it on MATLAB’s
path
Any return value which is not storedin an output variable is simply
discarded
Any return value which is not stored
in an output variable is simply
discarded
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 4/22
Problem 1
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 5/22
Problem 1
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 6/22
Solution 1
function sincomp(x,n)i = 1;tru = sin(x);ser = 0; //Value from Seriesfprintf('\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
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 7/22
Solution 1
>> sincomp (1.5,8)order true value approximation error1 0.9974949866 1.5000000000 -50.376695642 0.9974949866 0.9375000000 6.014565233 0.9974949866 1.0007812500 -0.329451624 0.9974949866 0.9973911830 0.010406435 0.9974949866 0.9974971226 -0.000214146 0.9974949866 0.9974949557 0.000003107 0.9974949866 0.9974949869 -0.00000003
8 0.9974949866 0.9974949866 0.00000000
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 8/22
Problem 2
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 9/22
Problem 2
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 10/22
Problem 2
l i
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 11/22
Solution 2function [r, th] = polar(x, y)r = sqrt(x .^ 2 + y .^ 2);
if x < 0
if y > 0 // second quadrant conditionth = atan(y / x) + pi;
elseif y < 0 // third quadrant conditionth = 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;
bl 3
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 12/22
Problem 3
P bl 3
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 13/22
Problem 3
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 14/22
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');
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 15/22
Solution 3A =
0.0350 0.0001 10.0000 2.00000.0200 0.0002 8.0000 1.00000.0150 0.0010 20.0000 1.50000.0300 0.0007 24.0000 3.00000.0220 0.0003 15.0000 2.5000
>> Manning(A)
n S B H U0.035 0.0001 10.00 2.00 0.36240.020 0.0002 8.00 1.00 0.60940.015 0.0010 20.00 1.50 2.51670.030 0.0007 24.00 3.00 1.58090.022 0.0003 15.00 2.50 1.1971
>>
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 16/22
Problem 4
• Economic formulas are available tocompute annual payments for loans.Suppose that you borrow an amount
of money P and agree to repay it inn annual payments at an interestrate of i. The formula to compute the
annual payment A is
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 17/22
Problem 4 (Contd..)
• Write an M-file to compute A. Test it with P = Rs.1,00,000.00 and aninterest rate of 6% (i = 0.06).
Compute results for n = 1, 2, 3, 4, and5 and display the results as a tablewith headings and columns for n and
A.
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 18/22
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);
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 19/22
Intro MATLAB
Variable Argument Lists (cont.)
• Consider the m-file:
function [s, varargout] = mysize(x)
nout = max(nargout,1) - 1;
s = size(x);
for k = 1:nout, varargout(k) = {s(k)}; end
• The following
>> [s,rows,cols] =mysize(rand(4,5))
returnss = [4 5], rows = 4, cols = 5
pack all outputvalues
into varargout cell
array
pack all outputvalues
into varargout cellarray
nargout: number of output
arguments in function call
nargout: number of output
arguments in function call
MATLAB FUN TI N F R BI E TI N
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 20/22
MATLAB FUN TI N F R BI E TI NMETHOD
function[root,fx,ea,iter]=bisection(func,xl,xu,es,maxit,varargin)% 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
MATLAB FUN TI N F R BI E TI N
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 21/22
MATLAB FUN TI N F R BI E TI NMETHOD
if nargin<3,error('at least 3 input arguments
required'),endtest =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;elsexu=xv;endtest =
func(xl,varargin{:})*func(xu,varargin{:});
MATLAB FUN TI N F R BI E TI N
8/2/2019 Discussion for Programming
http://slidepdf.com/reader/full/discussion-for-programming 22/22
MATLAB FUN TI N F R BI E TI NMETHODif nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 100; while (1)xrold = xr;xr = (xl + xu)/2;iter = iter + 1;if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});if test < 0
xu = xr;elseif test > 0xl = xr;
elseea = 0;
endif ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});