0% found this document useful (0 votes)
5 views

hw1_B09505021_Numerical_Analysis

The document outlines a numerical analysis homework assignment involving Lagrange interpolation and spline fitting using data from 'hw1AB.dat' and 'hw1CD.dat'. It includes MATLAB code snippets for reading data, calculating Lagrange polynomials, and constructing spline matrices. The results are visualized through plots generated from the computed values.

Uploaded by

Joseph Chang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

hw1_B09505021_Numerical_Analysis

The document outlines a numerical analysis homework assignment involving Lagrange interpolation and spline fitting using data from 'hw1AB.dat' and 'hw1CD.dat'. It includes MATLAB code snippets for reading data, calculating Lagrange polynomials, and constructing spline matrices. The results are visualized through plots generated from the computed values.

Uploaded by

Joseph Chang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

hw1 B09505021 Numerical Analysis

A.1

filename = "hw1AB.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);
x0 = datax;
y0 = datay;
x = linspace(-1, 1, 500)
d = size(x0, 1);
y = zeros(d, 500);
y = y+1;

for j = 1:d
for i = 1:d
if j == i
continue;
#skip and continue the loop
endif
y(j, :) .*= ((x-x0(i))/(x0(j)-x0(i)));
endfor
endfor

for k = 1:1:d
subplot(6, 2, k)
plot(x, y(k, :))
set(gca,'FontSize',10);
xlabel("X",'FontSize',15);
ylabel("Lj(x)",'FontSize',15);
title(x0(k),'FontSize',15);
grid on;
end

A.2

hw1 B09505021 Numerical Analysis 1


function [y] = lagrange(x, x0, y0)
# x-value we want to compute
# y-computed value
# x0-inputs
n = size(x0, 1);
# size of X0 in one dimension
y=0;
for j = 1:n
p = 1;
for i = 1:n
if j == i
continue;
#skip and continue the loop
endif
p .*= (x-x0(i))/(x0(j)-x0(i));
endfor
y += y0(j) * p;
endfor
endfunction

filename = "hw1AB.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);
x0 = datax;
y0 = datay;
x = linspace(-1, 1, 500)

y = lagrange(x, x0, y0);

figure
plot(x, y)

B.1

filename = "hw1AB.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);

hw1 B09505021 Numerical Analysis 2


x0 = datax;
y0 = datay;
n = size(x0, 1)
matrix = zeros(n, n);
f = zeros(n, 1);
delta = zeros(n, 1);
gsd = zeros(n, 1);

for i = 1:n-1
delta(i, 1) = x0(i+1)-x0(i);
endfor

for k = 1+1:n-1
for j = 1:n
if k == j+1
matrix(k, j) = delta(j, 1)./6
elseif k == j
matrix(k, j) = (delta(j, 1).+delta(j-1, 1))./3;
elseif k == j-1
matrix(k, j) = delta(j-1, 1)./6;
endif
endfor
endfor

for q = 1+1:n-1
f(q, 1) = (y0(q+1).-y0(q))./delta(q, 1).-(y0(q).-y0(q-1))./delta(q, 1)
endfor

gsd = matrix\f;
gsd(1, 1) = 0;
gsd(n, 1) = 0;

B.2

hw1 B09505021 Numerical Analysis 3


filename = 'hw1AB.dat'
[xj,yj]=textread(filename,'%f %f','HeaderLines',1)

n=length(xj);
g=zeros(n-2,n-2);
f=zeros(n-2,1);
G=zeros(n-2,1);
h=zeros(n-1,1);

for o=1:n-1;
h(o)=xj(o+1)-xj(o);
endfor;

for i=1:n-2;
for j=1:n-2;
if j==i-1;
g(i,j)=h(i)./6;
endif;
if j==i;
g(i,j)=(h(i+1).+h(i))./3;
endif;
if j==i+1;
g(i,j)=h(i+1)./6;
endif;
endfor;
endfor;

for k=1:n-2;
f(k,1)=((yj(k+2).-yj(k+1))./h(k+1)).-((yj(k+1).-yj(k))./h(k));
endfor;

G=g\f;

x=-1:0.0001:-0.8001;
x1=-0.8:0.0001:-0.6001;
x2=-0.6:0.0001:-0.4001;
x3=-0.4:0.0001:-0.2001;
x4=-0.2:0.0001:-0.0001;
x5=0:0.0001:0.1999;
x6=0.2:0.0001:0.3999;
x7=0.4:0.0001:0.5999;
x8=0.6:0.0001:0.8;
x9=0.8001:0.0001:1;

c=1;
c1=1;
c2=1;
c3=1;
c4=1;
c5=1;
c6=1;
c7=1;
c8=1;
c9=1;

c .*=G(1)./6.*(((x.-xj(1)).^3)./h(1).-(h(1).*(x.-xj(1)))) .+ ((yj(1).*(xj(2).-x).+yj(2).*(x.-xj(1)))./h(1));
c1 .*=(G(1)./6).*(((xj(3).-x1).^3)./h(2).-(h(2).*(xj(3).-x1))).+(G(2)./6).*(((x1.-xj(2)).^3)./h(2).-(h(2).*(x1.-xj(2)))).+((yj(2).*(xj(3)
c2 .*=(G(2)./6).*(((xj(4).-x2).^3)./h(3).-(h(3).*(xj(4).-x2))).+(G(3)./6).*(((x2.-xj(3)).^3)./h(3).-(h(3).*(x2.-xj(3)))).+((yj(3).*(xj(4)
c3 .*=(G(3)./6).*(((xj(5).-x3).^3)./h(4).-(h(4).*(xj(5).-x3))).+(G(4)./6).*(((x3.-xj(4)).^3)./h(4).-(h(4).*(x3.-xj(4)))).+((yj(4).*(xj(5)
c4 .*=(G(4)./6).*(((xj(6).-x4).^3)./h(5).-(h(5).*(xj(6).-x4))).+(G(5)./6).*(((x4.-xj(5)).^3)./h(5).-(h(5).*(x4.-xj(5)))).+((yj(5).*(xj(6)
c5 .*=(G(5)./6).*(((xj(7).-x5).^3)./h(6).-(h(6).*(xj(7).-x5))).+(G(6)./6).*(((x5.-xj(6)).^3)./h(6).-(h(6).*(x5.-xj(6)))).+((yj(6).*(xj(7)
c6 .*=(G(6)./6).*(((xj(8).-x6).^3)./h(7).-(h(7).*(xj(8).-x6))).+(G(7)./6).*(((x6.-xj(7)).^3)./h(7).-(h(7).*(x6.-xj(7)))).+((yj(7).*(xj(8)
c7 .*=(G(7)./6).*(((xj(9).-x7).^3)./h(8).-(h(8).*(xj(9).-x7))).+(G(8)./6).*(((x7.-xj(8)).^3)./h(8).-(h(8).*(x7.-xj(8)))).+((yj(8).*(xj(9)
c8 .*=(G(8)./6).*(((xj(10).-x8).^3)./h(9).-(h(9).*(xj(10).-x8))).+(G(9)./6).*(((x8.-xj(9)).^3)./h(9).-(h(9).*(x8.-xj(9)))).+((yj(9).*(xj(1
c9 .*=(G(9)./6).*(((xj(11).-x9).^3)./h(10).-(h(10).*(xj(11).-x9))) .+ ((yj(10).*(xj(11).-x9).+yj(11).*(x9.-xj(10)))./h(10));

y=c;
y1=c1;
y2=c2;
y3=c3;
y4=c4;
y5=c5;
y6=c6;
y7=c7;
y8=c8;
y9=c9;

plot(x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9);

hw1 B09505021 Numerical Analysis 4


set(gca,'FontSize',20);
xlabel("X",'FontSize',20);
ylabel("g(x)",'FontSize',20);
title("g(x) with 11 datas in hw1AB",'FontSize',20);
grid on;

C.1

filename = "hw1CD.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);
x0 = datax;
y0 = datay;
x = linspace(-1, 1, 500)
d = size(x0, 1);
y = zeros(d, 500);
y = y+1;

for j = 1:d

for i = 1:d
if j == i
continue;
#skip and continue the loop
endif
y(j, :) .*= ((x-x0(i))/(x0(j)-x0(i)));
endfor
endfor

for k = 1:1:d
subplot(6, 2, k)
plot(x, y(k, :))
set(gca,'FontSize',10);
xlabel("X",'FontSize',15);
ylabel("Lj(x)",'FontSize',15);
title(x0(k),'FontSize',15);
grid on;
end

hw1 B09505021 Numerical Analysis 5


C.2

function [y] = lagrange(x, x0, y0)


# x-value we want to compute
# y-computed value
# x0-inputs
n = size(x0, 1);
# size of X0 in one dimension
y=0;
for j = 1:n
p = 1;
for i = 1:n
if j == i
continue;
#skip and continue the loop
endif
p .*= (x-x0(i))/(x0(j)-x0(i));
endfor
y += y0(j) * p;
endfor
endfunction

filename = "hw1CD.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);
x0 = datax;
y0 = datay;
x = linspace(-1, 1, 500)

y = lagrange(x, x0, y0);

figure
plot(x, y)

hw1 B09505021 Numerical Analysis 6


D.1

filename = "hw1CD.dat";
[datax, datay] = textread(filename, "%f %f", 'headerlines', 1);
x0 = datax;
y0 = datay;
n = size(x0, 1)
matrix = zeros(n, n);
f = zeros(n, 1);
delta = zeros(n, 1);
gsd = zeros(n, 1);

for i = 1:n-1
delta(i, 1) = x0(i+1)-x0(i);
endfor

for k = 1+1:n-1
for j = 1:n
if k == j+1
matrix(k, j) = delta(j, 1)./6
elseif k == j
matrix(k, j) = (delta(j, 1).+delta(j-1, 1))./3;
elseif k == j-1
matrix(k, j) = delta(j-1, 1)./6;
endif
endfor
endfor

for q = 1+1:n-1
f(q, 1) = (y0(q+1).-y0(q))./delta(q, 1).-(y0(q).-y0(q-1))./delta(q-1, 1)
endfor

gsd = matrix\f;
gsd(1, 1) = 0;
gsd(n, 1) = 0;

hw1 B09505021 Numerical Analysis 7


D.2

filename = 'hw1CD.dat'
[xj,yj]=textread(filename,'%f %f','HeaderLines',1)

n=length(xj);
g=zeros(n-2,n-2);
f=zeros(n-2,1);
G=zeros(n-2,1);
h=zeros(n-1,1);
x=-1:0.0001:-0.9512;
x1=-0.9511:0.0001:-0.8091;
x2=-0.8090:0.0001:-0.5879;
x3=-0.5878:0.0001:-0.3091;
x4=-0.3090:0.0001:-0.0001;
x5=0:0.0001:0.3089;
x6=0.3090:0.0001:0.5877;
x7=0.5878:0.0001:0.8089;
x8=0.8090:0.0001:0.9511;
x9=0.9512:0.0001:1;

c=1;
c1=1;
c2=1;
c3=1;
c4=1;
c5=1;
c6=1;
c7=1;
c8=1;
c9=1;

for o=1:n-1;

hw1 B09505021 Numerical Analysis 8


h(o)=xj(o+1)-xj(o);
endfor;

for i=1:n-2;
for j=1:n-2;
if j==i-1;
g(i,j)=h(i)./6;
endif;
if j==i;
g(i,j)=(h(i+1).+h(i))./3;
endif;
if j==i+1;
g(i,j)=h(i+1)./6;
endif;
endfor;
endfor;

for k=1:n-2;
f(k,1)=((yj(k+2).-yj(k+1))./h(k+1)).-((yj(k+1).-yj(k))./h(k));
endfor

G=g\f;

c .*=G(1)./6.*(((x.-xj(1)).^3)./h(1).-(h(1).*(x.-xj(1)))) .+ ((yj(1).*(xj(2).-x).+yj(2).*(x.-xj(1)))./h(1));
c1 .*=(G(1)./6).*(((xj(3).-x1).^3)./h(2).-(h(2).*(xj(3).-x1))).+(G(2)./6).*(((x1.-xj(2)).^3)./h(2).-(h(2).*(x1.-xj(2)))).+((yj(2).*(xj(3)
c2 .*=(G(2)./6).*(((xj(4).-x2).^3)./h(3).-(h(3).*(xj(4).-x2))).+(G(3)./6).*(((x2.-xj(3)).^3)./h(3).-(h(3).*(x2.-xj(3)))).+((yj(3).*(xj(4)
c3 .*=(G(3)./6).*(((xj(5).-x3).^3)./h(4).-(h(4).*(xj(5).-x3))).+(G(4)./6).*(((x3.-xj(4)).^3)./h(4).-(h(4).*(x3.-xj(4)))).+((yj(4).*(xj(5)
c4 .*=(G(4)./6).*(((xj(6).-x4).^3)./h(5).-(h(5).*(xj(6).-x4))).+(G(5)./6).*(((x4.-xj(5)).^3)./h(5).-(h(5).*(x4.-xj(5)))).+((yj(5).*(xj(6)
c5 .*=(G(5)./6).*(((xj(7).-x5).^3)./h(6).-(h(6).*(xj(7).-x5))).+(G(6)./6).*(((x5.-xj(6)).^3)./h(6).-(h(6).*(x5.-xj(6)))).+((yj(6).*(xj(7)
c6 .*=(G(6)./6).*(((xj(8).-x6).^3)./h(7).-(h(7).*(xj(8).-x6))).+(G(7)./6).*(((x6.-xj(7)).^3)./h(7).-(h(7).*(x6.-xj(7)))).+((yj(7).*(xj(8)
c7 .*=(G(7)./6).*(((xj(9).-x7).^3)./h(8).-(h(8).*(xj(9).-x7))).+(G(8)./6).*(((x7.-xj(8)).^3)./h(8).-(h(8).*(x7.-xj(8)))).+((yj(8).*(xj(9)
c8 .*=(G(8)./6).*(((xj(10).-x8).^3)./h(9).-(h(9).*(xj(10).-x8))).+(G(9)./6).*(((x8.-xj(9)).^3)./h(9).-(h(9).*(x8.-xj(9)))).+((yj(9).*(xj(1
c9 .*=(G(9)./6).*(((xj(11).-x9).^3)./h(10).-(h(10).*(xj(11).-x9))) .+ ((yj(10).*(xj(11).-x9).+yj(11).*(x9.-xj(10)))./h(10));

y=c;
y1=c1;
y2=c2;
y3=c3;
y4=c4;
y5=c5;
y6=c6;
y7=c7;
y8=c8;
y9=c9;

plot(x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9);
set(gca,'FontSize',20);
xlabel("X",'FontSize',20);
ylabel("g(x)",'FontSize',20);
title("g(x) with 11 datas in hw1CD",'FontSize',20);
grid on;

hw1 B09505021 Numerical Analysis 9


hw1 B09505021 Numerical Analysis 10

You might also like