You are on page 1of 32

Amira Zakia Lutfi

142015100

Kelas B

NO 4)

EDITOR:
function[p,q,r,s]=natural_cubic_spline(t,y)
n=length(t)
p(n)=y(n)
a(1)=1
b(1)=0
c(1)=0
d(1)=0
ii=1;
while ii<=n-1
h(ii)=t(ii+1)-t(ii)
ii=ii+1
end
a(n)=1
b(n)=0
c(n)=0
d(n)=0
ii=2;
while ii<=n-1
a(ii)=2*(h(ii-1)+h(ii))
b(ii)=h(ii-1)
c(ii)=h(ii)
d(ii)=(3*(p(ii+1)-p(ii))/h(ii))-(3*(p(ii)-p(ii-1))/h(ii-1))
ii=ii+1;
end
[x]=Tridiagonal_Thomas (a,b,c,d)
ii=1
while ii<=n
r(ii)=x(ii)
ii=ii+1
end
ii=1
while ii<=n-1
s(ii)=(r(ii+1)-r(ii))/3*h(ii)
q(ii)=((p(ii+1)-p(ii))/h(ii))-(h(ii)*(2*r(ii)+r(ii+1))/3)
g(ii+1)=p(ii)+(q(ii)*h(ii))+(r(ii)*h(ii)^2)+(s(ii)*h(ii)^3)
Z(ii)=(2*r(ii))+(6*s(ii)*h(ii))
Z(ii)=0
ii=ii+1;
end
p
q
r
s

COMMAND WIDOW:

>> t=[ 2 4 7 11 16 21 24 26 30 35]


t=

Columns 1 through 6

2 4 7 11 16 21

Columns 7 through 10

24 26 30 35

>> y=[ 15 20 17 13 18 24 25 23 19 12]

y=

Columns 1 through 6

15 20 17 13 18 24

Columns 7 through 10

25 23 19 12

>> function[p,q,r,s]=natural_cubic_spline(t,y)

function[p,q,r,s]=natural_cubic_spline(t,y)

Error: Function definitions are not

permitted in this context.

>> [p,q,r,s]=natural_cubic_spline(t,y)
beta =

0 2

alfa =

1 10

ii =

beta =

0 2.0000 0.3000

alfa =

1.0000 10.0000 13.1000

ii =

beta =
Columns 1 through 3

0 2.0000 0.3000

Column 4

0.3053

alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Column 4

16.7786

ii =

beta =

Columns 1 through 3

0 2.0000 0.3000
Columns 4 through 5

0.3053 0.2980

alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 5

16.7786 18.5100

ii =

beta =

Columns 1 through 3

0 2.0000 0.3000

Columns 4 through 6

0.3053 0.2980 0.2701


alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 6

16.7786 18.5100 14.6494

ii =

beta =

Columns 1 through 3

0 2.0000 0.3000

Columns 4 through 6

0.3053 0.2980 0.2701

Column 7

0.2048
alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 6

16.7786 18.5100 14.6494

Column 7

9.3856

ii =

beta =

Columns 1 through 3

0 2.0000 0.3000

Columns 4 through 6

0.3053 0.2980 0.2701


Columns 7 through 8

0.2048 0.2131

alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 6

16.7786 18.5100 14.6494

Columns 7 through 8

9.3856 11.5738

ii =

beta =

Columns 1 through 3

0 2.0000 0.3000
Columns 4 through 6

0.3053 0.2980 0.2701

Columns 7 through 9

0.2048 0.2131 0.3456

alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 6

16.7786 18.5100 14.6494

Columns 7 through 9

9.3856 11.5738 16.6176

ii =

10

beta =
Columns 1 through 3

0 2.0000 0.3000

Columns 4 through 6

0.3053 0.2980 0.2701

Columns 7 through 9

0.2048 0.2131 0.3456

Column 10

alfa =

Columns 1 through 3

1.0000 10.0000 13.1000

Columns 4 through 6

16.7786 18.5100 14.6494

Columns 7 through 9

9.3856 11.5738 16.6176


Column 10

1.0000

ii =

11

y=

ii =

y=

0 0

ii =

3
y=

0 0 0

ii =

y=

0 0 0 0

ii =

y=

0 0 0 0 0

ii =

y=
0 0 0 0 0 0

ii =

y=

Columns 1 through 6

0 0 0 0 0 0

Column 7

ii =

y=

Columns 1 through 6

0 0 0 0 0 0
Columns 7 through 8

0 0

ii =

y=

Columns 1 through 3

0 0 0

Columns 4 through 6

0 0 0

Columns 7 through 9

0 0 7.2000

ii =

10

y=
Columns 1 through 3

0 0 0

Columns 4 through 6

0 0 0

Columns 7 through 9

0 0 7.2000

Column 10

ii =

11

x=

Columns 1 through 6

0 0 0 0 0 0

Columns 7 through 10
0 0 0 0

ii =

x=

Columns 1 through 3

0 0 0

Columns 4 through 6

0 0 0

Columns 7 through 9

0 0 0.4333

Column 10

ii =

8
x=

Columns 1 through 3

0 0 0

Columns 4 through 6

0 0 0

Columns 7 through 9

0 -0.1497 0.4333

Column 10

ii =

x=

Columns 1 through 3

0 0 0
Columns 4 through 6

0 0 0

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

ii =

x=

Columns 1 through 3

0 0 0

Columns 4 through 6

0 0 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333


Column 10

ii =

x=

Columns 1 through 3

0 0 0

Columns 4 through 6

0 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

ii =
4

x=

Columns 1 through 3

0 0 0

Columns 4 through 6

-0.0005 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

ii =

x=

Columns 1 through 3
0 0 0.0002

Columns 4 through 6

-0.0005 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

ii =

x=

Columns 1 through 3

0 -0.0000 0.0002

Columns 4 through 6

-0.0005 0.0018 -0.0065


Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

ii =

x=

Columns 1 through 3

0 -0.0000 0.0002

Columns 4 through 6

-0.0005 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

0
ii =

x=

-0.0000

0.0002

-0.0005

0.0018

-0.0065

0.0319

-0.1497

0.4333

x=

-0.0000

0.0002

-0.0005

0.0018

-0.0065

0.0319

-0.1497
0.4333

p=

Columns 1 through 6

0 0 0 0 0 0

Columns 7 through 10

0 0 0 12

q=

Columns 1 through 3

0.0000 -0.0001 0.0003

Columns 4 through 6

-0.0012 0.0050 -0.0188

Columns 7 through 9

0.0573 -0.1784 0.9557

r=
Columns 1 through 3

0 -0.0000 0.0002

Columns 4 through 6

-0.0005 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

s=

Columns 1 through 3

-0.0000 0.0002 -0.0009

Columns 4 through 6

0.0038 -0.0138 0.0384

Columns 7 through 9

-0.1211 0.7774 -0.7221


p=

Columns 1 through 6

0 0 0 0 0 0

Columns 7 through 10

0 0 0 12

q=

Columns 1 through 3

0.0000 -0.0001 0.0003

Columns 4 through 6

-0.0012 0.0050 -0.0188

Columns 7 through 9

0.0573 -0.1784 0.9557

r=

Columns 1 through 3
0 -0.0000 0.0002

Columns 4 through 6

-0.0005 0.0018 -0.0065

Columns 7 through 9

0.0319 -0.1497 0.4333

Column 10

s=

Columns 1 through 3

-0.0000 0.0002 -0.0009

Columns 4 through 6

0.0038 -0.0138 0.0384

Columns 7 through 9

-0.1211 0.7774 -0.7221


NO2)

EDITOR:
function[Ca]=bhingga_maju(fungsi,l,m,n,t,alfa)
fungsi=@(x)4*x-2*x^2
l=2
m=2
n=2
alfa=1.5
t=2
h=l/m;
k=t/n;
lamda=(alfa^2*k)/h^2;
ii=1
while ii<=m+1
x(ii+1)=ii*h;
C(ii,1)=fungsi(x(ii));
ii=ii+1;
end
ii=1;
while ii<=m+1
A(ii,ii)=1-2*lamda;
A(ii+1,ii)=lamda;
A(ii,ii+1)=lamda;
ii=ii+1;
end
ii=1;
while ii<=m+1
CC(ii)=C(ii,1);
ii=ii+1;
end
ii=1;
while ii<=m+1
jj=1;
Ca(ii)=0;
while jj<=n+1
Ca(ii)=Ca(ii)+CC(ii)*A(ii,jj);
jj=jj+1;
end
ii=ii+1;
end
Ca

COMMAND WINDOW:

bhingga_maju

fungsi =

@(x)4*x-2*x^2
l=

m=

n=

alfa =

1.5000

t=

ii =

Ca =
0 2 0

ans =

0 2 0

>>
NO 3)

EDITOR:
NO 1)

EDITOR:

You might also like