Vous êtes sur la page 1sur 5

Differenciation

clc
clear all
close all
format short
%derivative
k=3;
h=0.1;%small change of x
x=2;% value of x at which the derai vative would be found
f=@(x)1./x;% the function
for i=1:k
h(i)=10.^(-i);
tpf(i)=(f(x+h(i))-f(x))./(h(i));
tpc(i)=(f(x+h(i))-f(x-h(i)))./(2*h(i));

%%exact valu calculation
y=1./x;
y1=diff(y);
df=@(x)-1./(x.^2);
%error
error1(i)=abs(tpf(i)-df(2));
error2(i)=abs(tpc(i)-df(2));
end
[h' tpf' tp

Intregation trapezoid
clear all
close all
clc
format long;
a=1; %lower lomit
b=2; %upper limit
h=b-a; %difference between lower and upper limit
f=@(x) (exp(x)); %the integrand
%approximation
trp=(h./2)*(f(a)+f(b));
%exact value calculation
exact=quad(f,a,b)
%%%%%error calculation
error=abs(trp-exact);

Composite trapisoit
clear all
close all
clc
%trapizoid
a=1;
b=2;
m=4;%number of pannel
h=(b-a)./m ;
f=@(x)exp(x)'
sum=0;
for i=1:m-1
sum=2*f(a+i*h)+sum;
end

com_trp= (f(a)+sum+f(b))*(h./2);
trap= (h./2)*(f(a)+f(b));
%excat
excat=quad(f,a,b)
com_trp'

Simpson
clear all
close all
clc
format long;
a=0; %lower limit
b=1; %upper limit
h=b-a; %difference between lower and upper limit
h1=(b-a)./2;
c=a+h1;
f=@(x) (exp(x)) %the integrand
%approximation
simp=(h1./3)*(f(a)+4*f(c)+f(b))
%summation calculation
exact=quad(f,a,b);
%%%%%error calculation
error_simp=abs(simp -exact)


composite simp son

clear all
close all
clc
format long
m=3; %number of panels
a=1; %lower limit
b=2; %upper limit
h=(b-a)./(2*m); %distance between two points
f=@(x) (log(x)); %the integrand
%Approximation
sum1=0;
for i=1:m
sum1=sum1+4*f((a+(2*i-1)*h));
end
sum2=0
for i=1:m-1
sum2=sum2+2*f(a+2*i*h);
end
compsimp=(h./3)*(f(a)+sum1+sum2+f(b));
%exact value calculation
ext=quad(f,a,b)
%Error calculation
error=abs(compsimp-ext)






Newton

clear all
close all
clc
format long
%h=0.5 %the step size
h=0.1 %the step size
t=0:h:2;
m=length(t); %number of grids
f=@(t,y) y; %right side of the derivative
y(1)=1;
for i=1:m-1
y(i+1)=y(i)+h*f(t(i),y(i));
end
y'
%the ecact value
z=exp(t);
z'
%error calculation
error=abs(y'-z')
%plot of exact and approximate solution
%plot(t,y,'r',t,z,'g')
plot(t,y,'-*',t,z,'-d')
grid on



%%Newton devided difference formula
function c=newtdd(x,y,n)
for i=1:n
v(i,1)=y(i);
end
v;
for i=2:n
j=1:n+1-i
v(j,i)=(v(j+1,i-1)-v(j,i-1))./(x(j+i-1)-x(j));
end
end
for i=1:n
c(i)=v(1,i);
end
end

kutta
clear all
close all
clc
h=0.5;
t=0:h:2;
y(1)=1;
m=length(t);
f=@(t,y) (y);
for i=1:m-1
s1=f(t(i),y(i));
s2=f(t(i)+(h./2),y(i)+(h./2)*s1);
s3=f(t(i)+(h./2),y(i)+(h./2)*s2);
s4=f(t(i)+h,y(i)+h*s3);
y(i+1)=y(i)+(h./6)*(s1+2*s2+2*s3+s4);
end
y'
z=exp(t);
z'
error=abs(y'-z')
plot(t,y,'r',t,z,'g')


regretion
clear all
close all
clc
x=1:7;
y=[0.5,2.5,2,4,3.5,6,5.5];
n=7;
a1=(n*sum(x.*y)-sum(x)*sum(y))./(n*sum(x.^2)-sum(x).^2)
a0=mean(y)-a1*mean(x);
z=0:0.01:10;
w=a0+a1*z;
plot(z,w,'r',x,y,'o')


clc
close all
clear all
format long
tol=10.^(-4);
A=[3,1,-1;2,4,1;-1,2,5];%coeffecient matrix
B=[4;1;1];%right hand side colug matrix
N=length(B);
k=50;%number of iterations
d=diag(diag(A));
U=triu(A,1);%upper triangular
L=tril(A,-1);%lower triangular
x=zeros(N,1);
for i=1:k
bL=B-U*x0
for j=1:N
x(j)=(bL(j)-L(j,:)*x)./d(j,j);
end
if norm(A*x-B,inf)==0
break
end
end
x

Vous aimerez peut-être aussi