Sei sulla pagina 1di 16

2.

Jawaban:

Matlab Code:

Linier Regresi

function [a, r2] = linregr(x,y)


% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy = sum(x.*y); sy2 = sum(y.*y);
a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(2) = sy/n-a(1)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(x),max(x),2);
yp = a(1)*xp+a(2);
plot(x,y,'o',xp,yp)
grid on

Linear Saturasi

%saturasi
function [a, r2] = linsat(x,y)
% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors

sx=sum(1./x);
sy=sum(1./y);
sx2=sum((1./x).*(1./x));
sxy=sum((1./x).*(1./y));
sy2=sum((1./y).*(1./y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=1/a(2);
yp=b*(x/((b*a(1))+x));
plot(x,y,'o',x,yp);
grid on
Linear Power

%power
function [a, r2] = linlog(x,y)
n = length(x);
if length (y)~=n, error ('x and y must be same length'); end;
x=x(:); y=y(:);

sx=sum(log10(x));
sy=sum(log10(y));
sx2=sum(log10(x).*log10(x));
sxy=sum(log10(x).*log10(y));
sy2=sum(log10(y).*log10(y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=10.^(a(2));
yp=b.*(x.^(a(1)));

plot(x,y,'o',x,yp,'r')
grid on

Linear exponensial

%eksponensial
function [b, a, r2] = linexp(x,y)
% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);

if length (y)~=n, error ('x and y must be same length');end;


x=x(:);
y=y(:);

sx=sum(x);
sy=sum(log(y));
sx2=sum(x.*x);
sxy=sum(x.*log(y));
sy2=sum(log(y).*log(y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=exp(a(2));
yp=b*exp(a(1).*x);
plot(x,y,'o',x,yp)
grid on
Command Window:
>> x=[2 3 4 5 6 7];
>> y=[0.5 0.333 0.25 0.2 0.1667 0.1429];
>> plot(x,y)
>> linregr
Error using linregr (line 7)
Not enough input arguments.

>> [a, r2] = linregr(x,y)

a=

-0.0667 0.5656

r2 =

0.8760
>> linsat
Error using linsat (line 8)
Not enough input arguments.

>> [a, r2] = linsat(x,y)

a=

-13.1256 7.9845

r2 =

0.8767
>>linlog
Error using linlog (line 3)
Not enough input arguments.

>> [a, r2] = linlog(x,y)

a=

-0.9995 -0.0004

r2 =
1.0000

>> linexp
Error using linexp (line 8)
Not enough input arguments.

>> [b, a, r2] = linexp(x,y)

b=

0.7260

a=

-0.2446 -0.3202

r2 =

0.9675

a) Hasil Persamaan:

Regresi linear : 𝑦 = −0.0667 + 0.5656x

Linear power: log 𝑦 = 𝑙𝑜𝑔 − 0.9995 − 0.0004 logx

𝑦 = 10−0.0004 𝑥 −0.9995

Y= 0,999𝑥 −0.9995

0,23= 0,999𝑥 −0.9995

X= 4,3

b) Nilai R kuadra:
linear saturasi power eksponensial
R^2 0.8760 0.8767 1.0000 0.9675

c) Y=0.23, x?
Linear: 𝑦 = −0.0667 + 0.5656x
0,23 = -0.0667 + 0.565x
X= 5.031
Power: Y= 0,999𝑥 −0.9995

0,23= 0,999𝑥 −0.9995

X= 4,3

0.5

0.45

0.4

0.35

0.3

0.25

0.2

0.15

0.1

0.05

0
2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7
1. Jawaban:

Matlab Code:

Linier Regresi

function [a, r2] = linregr(x,y)


% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy = sum(x.*y); sy2 = sum(y.*y);
a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(2) = sy/n-a(1)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(x),max(x),2);
yp = a(1)*xp+a(2);
plot(x,y,'o',xp,yp)
grid on

Linear Saturasi

%saturasi
function [a, r2] = linsat(x,y)
% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors

sx=sum(1./x);
sy=sum(1./y);
sx2=sum((1./x).*(1./x));
sxy=sum((1./x).*(1./y));
sy2=sum((1./y).*(1./y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=1/a(2);
yp=b*(x/((b*a(1))+x));
plot(x,y,'o',x,yp);
grid on
Linear Power

%power
function [a, r2] = linlog(x,y)
n = length(x);
if length (y)~=n, error ('x and y must be same length'); end;
x=x(:); y=y(:);

sx=sum(log10(x));
sy=sum(log10(y));
sx2=sum(log10(x).*log10(x));
sxy=sum(log10(x).*log10(y));
sy2=sum(log10(y).*log10(y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=10.^(a(2));
yp=b.*(x.^(a(1)));

plot(x,y,'o',x,yp,'r')
grid on

Linear exponensial

%eksponensial
function [b, a, r2] = linexp(x,y)
% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input: x = independent variable; y = dependent variable
% output: a = vector of slope, a(1), and intercept, a(2); r2 = coefficient of
determination
n = length(x);

if length (y)~=n, error ('x and y must be same length');end;


x=x(:);
y=y(:);

sx=sum(x);
sy=sum(log(y));
sx2=sum(x.*x);
sxy=sum(x.*log(y));
sy2=sum(log(y).*log(y));

a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); %beta


a(2) = sy/n-a(1)*sx/n; %alfa
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;

b=exp(a(2));
yp=b*exp(a(1).*x);
plot(x,y,'o',x,yp)
grid on
Command Window:
>> x=[1 2 3 4 5];
>> y=[0.5 0.8 0.9 0.941176 0.961538];
>> plot(x,y)

0.95

0.9

0.85

0.8

0.75

0.7

0.65

0.6

0.55

0.5
1 1.5 2 2.5 3 3.5 4 4.5 5

>> linregr
Error using linregr (line 7)
Not enough input arguments.

>> [a, r2] = linregr(x,y)

a=

0.1064 0.5013

r2 =

0.7870
>> linsat
Error using linsat (line 8)
Not enough input arguments.
>> [a, r2] = linsat(x,y)

a=

1.2290 0.7315

r2 =

0.9752
>> linlog
Error using linlog (line 3)
Not enough input arguments.

>> [a, r2] = linlog(x,y)

a=

0.4062 -0.2663

r2 =

0.9036
>> linexp
Error using linexp (line 8)
Not enough input arguments.

>> [b, a, r2] = linexp(x,y)

b=

0.5141

a=

0.1470 -0.6654

r2 =

0.7329
1.4

1.2

0.8

0.6

0.4

0.2

0
1 1.5 2 2.5 3 3.5 4 4.5 5
4.jawaban:
Matlab Code:
Eksplisit

function U=eksplisit(a,b,c,d,f,e1,e2,m,n)
%U=eksplisit(0.05,9,1.4129E-5,20,100,25,4,6)
%a panjang total
%b waktu total
%f konstanta
c=1.4129*10^-5;
h=a/(n-1); %delta x
k=b/(m-1); %delta t
r=c*k/h^2; %lambda, c=alpha
s=k/h;
T=zeros(n,m);
T(1,1:m)=e1; %batas kiri
T(n,1:m)=e2 %batas kanan
T(2:n-1,1)=d %batas di tengah

for j=2:m
for i=2:n-1
T(i,j)=r*(T(i+1, j-1)-2*T(i,j-1)+T(i-1,j-1))+T(i,j-1)-
f*k*(T(i,j-1));
%perhitungan finite difference dari penurunan deret taylor-nya
end
end

U=T';
figure(1)
surf(U)
xlabel('t')
ylabel('x')
zlabel('U')

figure(2)
xx=(0:0.01:0.05);
y1=U(2,1:n)
y2=U(3,1:n)
y3=U(4,1:n);

plot(xx,y1)
hold on
plot(xx,y2)
hold on
plot(xx,y3)
%axis (0:0.001:0.05)
xlabel('panjang (m)')
ylabel('temperatur (t)')
legend('Time: 3s' , 'Time: 6s', 'Time: 9s')

Command window:
>> eksplisit
Error using eksplisit (line 7)
Not enough input arguments.
>> U=eksplisit(0.05,9,1.4129E-5,30,100,30,4,6)
Error using eksplisit (line 7)
Not enough input arguments.

>> U=eksplisit(0.05,9,1.4129E-5,30,0.5,100,30,4,6)

T=

100 100 100 100


0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
30 30 30 30

T=

100 100 100 100


30 0 0 0
30 0 0 0
30 0 0 0
30 0 0 0
30 30 30 30

y1 =

100.0000 14.6709 -15.0000 -15.0000 -15.0000 30.0000

y2 =

100.0000 16.2564 20.0766 7.5000 26.5741 30.0000

U=

100.0000 30.0000 30.0000 30.0000 30.0000 30.0000


100.0000 14.6709 -15.0000 -15.0000 -15.0000 30.0000
100.0000 16.2564 20.0766 7.5000 26.5741 30.0000
100.0000 28.9875 -16.9884 9.6658 -19.9199 30.0000

Kurva
100
Time: 3s
Time: 6s
80 Time: 9s

60
temperatur (t)

40

20

-20
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
panjang (m)

Matlab Code:
Implisit

function
U=implisit(panjang,waktu,lamda,suhutengah,suhuawal1,suhuawal2,t,l);
%U=eksplisit(0.05 ,9,1.4129E-5,20,100,25,4,6)

U=zeros(t,1);
R=zeros(l,l);
h=panjang/(l-1);
k=waktu/(t-1);
r=lamda*k/(h^2)
s1=1+2*r;
x=1-(0.5*k);
R(1,1)=1;
R(l,l)=1;
T(2:l-1,1)=suhutengah;
T(1,1)=suhuawal1;
T(l,1)=suhuawal2;
v=T';
U(1,1:l)=v;
for j=2:t
for i=2:l-1
R(i,i-1)=-1*r/(1-0.5*k);
R(i,i)=s1/(1-0.5*k);
R(i,i+1)=(-1*r)/(1-0.5*k);
end
T=R\T;
v=T';
U(j,1:l)=v;
end
figure(1)
surf(U)
xlabel('t')
ylabel('x')
zlabel('U')
end

Command window:
>> implisit
Error using implisit (line 4)
Not enough input arguments.

>> U=implisit(0.05,9,1.4129E-5,30,100,30,4,6)

r=

0.4239

Error using f (line 2)


Not enough input arguments.

Error in implisit (line 20)


R(i,i-1)=-1*r/(1-f*k);

>> implisit
Error using implisit (line 4)
Not enough input arguments.

>> U=implisit(0.05,9,1.4129E-5,30,100,30,4,6)

r=

0.4239

U=

100.0000 30.0000 30.0000 30.0000 30.0000 30.0000


100.0000 13.0851 -7.5713 -10.7016 -3.6910 30.0000
100.0000 21.3637 8.5643 7.0387 9.4954 30.0000
100.0000 17.5182 1.5665 -0.5869 4.1779 30.0000
Kurva

100

80

60

40
U

20

-20
4
6
3
5
4
2 3
2
x 1 1
t
3.jawaban:

Potrebbero piacerti anche