MAE 502 Prob Set 3
MAE 502 Prob Set 3
Engineering Analysis II
Problem Set No. 3
11/04/2014
Ong, Anthony
4989
11.95
%Define the entries of the coefficient matrix and the right-hand side
%vecotr as inline function
A11 = inline('2*x','x','y'); A12 = inline('2*y','x','y');
A21 = inline('4*x','x','y'); A22 = inline('6*y','x','y');
f1 = inline('x^2+y^2-1.2','x','y');
f2 = inline('2*x^2+3*y^2 - 3','x','y');
tol = 1e-4; kmax = 10;
v(:,1) = [0.25;1];
for k = 1:kmax,
A = [A11(v(1,k),v(2,k)) A12(v(1,k),v(2,k)); A21(v(1,k),v(2,k))
A22(v(1,k),v(2,k))];
b = [-f1(v(1,k),v(2,k));-f2(v(1,k),v(2,k))];
% Check to see if a root has been found, while two successive vectors do
% not satisfy terminating condition due to the nature of function(s) near a root
if abs(b(1)) < tol && abs(b(2)) < tol,
root = v(:,k);
return
end
delv = A\b;
v(:,k+1) = v(:,k) + delv;
%update solution
1.3250
0.8889
0.7819
0.7746
0.7746
1.0000
0.8000
0.7750
0.7746
0.7746
0.7746
12.16
function [a2, a1, a0] = QuadraticRegression(x,y)
% The user-defined function calls [a2 a1 a0] = QuadraticRegression(x,y)
% The method uses the quadratic least-squares regression approach to find
% the second-degree polynomial that best fits a set of data.
Sumx = sum(x); Sumxx = sum(x.*x); Sumx3 = sum(x.^3); Sumxxxx = sum(x.^4);
Sumxy = sum(x.*y); Sumx2y = sum(x.^2.*y);
Sumy=sum(y);
n=length(x);
A = [n Sumx Sumxx;Sumx Sumxx Sumx3;Sumxx Sumx3 Sumxxxx];
b = [Sumy;Sumxy;Sumx2y];
c=A\b;
a0 = c(1,1); a1 = c(2,1); a2 = c(3,1);
% for i = 1:n,
%
l(1) =a2*x(i)^2+ a1*x(i) + a0;
% end
xnew=linspace(0,1.6);
m=length(xnew);
l = zeros(100,1);
for i = 1:m,
l(i) =a2*xnew(i)^2+ a1*xnew(i) + a0;
end
plot (x,y,'o')
hold on
plot (xnew,l)
end
12.19
function [a1 a0] = LinearRegression(x,y)
n = length(x);
Sumx = sum(x); Sumy = sum(y); Sumxx = sum(x.*x); Sumxy = sum(x.*y);
den = n*Sumxx - Sumx^2;
a1 = (n*Sumxy - Sumx*Sumy)/den;
a0 = (Sumxx*Sumy - Sumxy*Sumx)/den;
l = zeros(n,1);
for i = 1:n,
l(i) = a1*x(i) + a0;
end
plot (x,y,'o')
hold on
plot (x,l)
end
function [a2, a1, a0] = QuadraticRegression(x,y)
% The user-defined function calls [a2 a1 a0] = QuadraticRegression(x,y)
% The method uses the quadratic least-squares regression approach to find
% the second-degree polynomial that best fits a set of data.
Sumx = sum(x); Sumxx = sum(x.*x); Sumx3 = sum(x.^3); Sumxxxx = sum(x.^4);
Sumxy = sum(x.*y); Sumx2y = sum(x.^2.*y);
Sumy=sum(y);
n=length(x);
A = [n Sumx Sumxx;Sumx Sumxx Sumx3;Sumxx Sumx3 Sumxxxx];
b = [Sumy;Sumxy;Sumx2y];
c=A\b;
a0 = c(1,1); a1 = c(2,1); a2 = c(3,1);
% for i = 1:n,
%
l(1) =a2*x(i)^2+ a1*x(i) + a0;
% end
xnew=linspace(min(x),max(x));
m=length(xnew);
l = zeros(m,1);
for i = 1:m,
l(i) =a2*xnew(i)^2+ a1*xnew(i) + a0;
end
plot (x,y,'o')
hold on
plot (xnew,l)
end
>> x = [0 1 2 3 4 5]; y = [2 4.1 5.5 6.7 8.8 9.1];
>> [a1 a0] = LinearRegression(x,y)
a1 =
1.4514
a0 =
2.4048
a2 =
-0.1107
a1 =
2.0050
a0 =
2.0357
12.36
function yi = NewtonInterp(x,y,xi)
%
n = length(x); a = zeros(1,n);
a(1) = y(1);
DivDiff = zeros (1,n-1);
for i = 1:n-1,
DivDiff(i,1) = (y(i+1) - y(i))/(x(i+1) - x(i));
end
for j = 2:n-1,
for i = 1:n-j,
DivDiff(i,j) = (DivDiff(i+1,j-1) - DivDiff(i,j-1))/(x(j+i) - x(i));
end
end
for k = 2:n,
a(k) = DivDiff(1,k-1);
end
yi = a(1);
xprod = 1;
for m= 2:n,
xprod = xprod*(xi-x(m-1));
yi = yi + a(m)*xprod;
end
>> x = [1 1.5 2.5 3 4 4.5 5 6]; y = [1.3956 1.6487 2.3010 2.7183 3.7937 4.4817 5.2945
7.3891];
>> format long
>> yi = NewtonInterp(x,y,4.25)
yi =
4.123376224998191