=
= + + =
+ + ~ =
}
c
| |
% 71 . 30
926 . 5216
209 . 6819 926 . 5216
209 . 6819 832 . 11923 ) 33933 . 552 ( 3 ) 18922 . 19 ( 3 0
8
) 4/3 ( 3
) 4 ( f )
3
8
( f 3 )
3
4
( f 3 ) 0 ( f
8
h 3
dx xe I
4
0
x 2
=
=
= + + + =
(
+ + + ~ =
}
c
15 May 2011 ME262 Numerical Analysis Sessional
14
MATLAB Code
dx xe I
4
0
x 2
}
=
function s = fvalue(x)
s = x*exp(2*x);
a = 0; b = 4; n = 6;
h = (b - a)/n;
S = 0; m = n/3;
for k = 1 : m
S = S + fvalue(a+h*(3*k-3))+(3*fvalue(a+h*(3*k-2)))+(3*fvalue(a+h*(3*k-1)))+fvalue(a+h*3*k);
end
I = (3*h*S)/8;
disp(I);
fvalue.m
simpsn38.m
15 May 2011 ME262 Numerical Analysis Sessional
15
MATLAB Code (Combined Rules)
x=[0.5 2 3 4 6 8 10 11 ]
f=[320 268 233 226 226 212 142 107 ]
n=8;
h =x(2) - x(1);
k = 1;
sum = 0;
for j =2:n
if j~=n
hf = x(j+1) - x(j);
else
hf = abs(h+1); % this is for last value
end
if abs(h-hf)<0.000001
if k == 3
sum = sum + (h/3)*(f(j-3)+(4*f(j-2))+f(j-1));
k = k-1;
else
k = k+1;
end
else
15 May 2011 ME262 Numerical Analysis Sessional
16
MATLAB Code
if k ==1
sum = sum + (h/2)*(f(j-1)+f(j));
else
if k==2
sum = sum + (h/3)*(f(j-2)+(4*f(j-1))+f(j));
else
sum = sum + ((3*h)/8)*(f(j-3)+(3*f(j-2))+(3*f(j-1))+f(j));
end
k=1;
end
end
h = hf;
end
disp(sum);
15 May 2011 ME262 Numerical Analysis Sessional
17
MATLAB Functions
Command Description
quad (function,a,b,tol) Uses an adaptive Simpsons rule to compute the integral of
the function function with a as the lower integration limit
and b as the upper limit. The parameter tol is optional. tol
indicates the specified error tolerance.
trapz (x,y) Uses trapezoidal integration to compute the integral of y
with respect to x, where the array y contains the function
values at the points contained in the array x.
(use of the trapz function)
>> x = linspace(0, pi, 10);
>> y = sin(x);
>> trapz(x, y)
ans =
1.97965081121648
(use of the quad function)
>> quad('sin(x)', 0, pi, 0.001)
ans =
1.99999349653496
Curve Fitting
15 May 2011
18
ME262 Numerical Analysis Sessional
15 May 2011 ME262 Numerical Analysis Sessional
19
MATLAB Functions
Command Description
polyfit(x,y,n) Two vectors x and y fits a polynomial of order n through
the data points (x
i
,y
i
) and returns (n+1) coefficients of the
powers of x in the vector a. The coefficients are arranged
in the decreasing order of the powers of x, i.e.,
a = [a
n
a
n-1
. a
1
a
0
]
polyval(a,x) A data vector x and the coefficients of a polynomial in a
row vector a, the command y = polyval(a,x) evaluates the
polynomial at the data points x
i
.
15 May 2011 ME262 Numerical Analysis Sessional
20
Example
x = 5:5:50;
y = [16 25 32 33 38 36 39 40 42 42];
p = polyfit(x,y,2); % fit a line (2nd order polynomial)
F_fit = polyval(p,x); % evaluate the polynomial at new points
plot(x,y,'o',x,F_fit); % plot the data and fitted curve
xlabel('x');
ylabel('y');
title ('The variation of x with y');
x 5 10 15 20 25 30 35 40 50
y 16 25 32 33 38 36 39 49 42
Example (Contd.)
15 May 2011 ME262 Numerical Analysis Sessional
21
15 May 2011 ME262 Numerical Analysis Sessional
22
Example
% Power curve fit example y = a x^b ---- log(x) = log(a)+ b log(y)
x = 5:5:50;
y = [16 25 32 33 38 36 39 40 42 42];
xbar = log10(x);
ybar = log10(y);
p = polyfit(xbar,ybar,1); % fit a line (1st order polynomial) p=[p1 p0]
a = 10^p(2); % since p(2) is p0
ynew = a.*(x).^p(1);
plot(x,y,'o',x,ynew); % plot the data and fitted curve
xlabel('x');
ylabel('y');
title ('The variation of x with y');
disp([p]);
disp([p(1) a]);
x 5 10 15 20 25 30 35 40 50
y 16 25 32 33 38 36 39 49 42
THANK YOU
5/15/2011
23
Its all about today!!