CLC Clear H 0.1 y (1) 1 X (1) 0 Xfinal 1.8 I 1 : Function Euler
CLC Clear H 0.1 y (1) 1 X (1) 0 Xfinal 1.8 I 1 : Function Euler
clc;clear all;
h=0.1; y(1)=1;x(1)=0; xfinal=1.8; i=1;
for k=x(1):h:xfinal-h
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*fonk(x(i),y(i));
i=i+1;
end
y2=x.^2-exp(0.5*x.^2)+2;
plot(x,y,'m-o');hold on;
plot(x,y2,'Linewidth',3);
function f=fonk(x,y)
f=x*y-x^3;
function Newton
i=1;
x(1)=10;
eps=0,001;
error=10;
while abs(error)>eps
x(i+1)=x(i)-fonk(x(i))/dfonk(x(i));
error=x(i+1)-x(i);
i=i+1;
end
x'
function f=fonk(x)
f=x^2-3*x-4;
function df=dfonk(x)
df=2*x-3
for i=1:n
A(i,:)=A(i,:)/A(i,i);
disp('A='); disp(A); pause;
for k=i+1:
dd=A(k,i);
A(k,:)=A(k,:)-dd*A(i,:); A
pause;
end
end
for i=n:-1:2
for k=i-1:-1:1
dd=A(k,i);
A(k,:)=A(k,:)-dd*A(i,:); A
pause
end
end
x=A(:,col);
disp(' The unknown are:'); disp(x);
plot(xx,yy4,'m:')
xx=1:0.1:12;
c=polyfit(x,y,4);
yy4=c(1)*xx.^4+c(2)*xx.^3+c(3)*xx.^2+c(4)*xx+c(5);
plot(xx,yy4,'m:')
c=polyfit(x,y,5);
yy5=c(1)*xx.^5+c(2)*xx.^4+c(3)*xx.^3+c(4)*xx.^2+c(5)*xx+c(6);
plot(xx,yy5,'r:')
sx=0;sxx=0;sxy=0;sy=0; [a b]=size(x);
for i=1:1:b
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxy=sxy+x(i)*y(i);
end
a1=(b*sxy-sx*sy)/(b*sxx-sx^2)
a0=(sxx*sy-sxy*sx)/(b*sxx-sx^2)
plot(x,y,'ro');hold on;
xx=-2:0.1:12;
yy=a1*xx+a0;
plot(xx,yy);
c=polyfit(x,y,6);
1yy6=c(1)*xx.^6+c(2)*xx.^5+c(3)*xx.^4+c(4)*xx.^3+c(5)*xx.^2+c(6)*xx+c(
7);
plot(xx,yy6,'b:')