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

CLC Clear H 0.1 y (1) 1 X (1) 0 Xfinal 1.8 I 1 : Function Euler

This document contains code for several mathematical functions and examples: 1) It defines functions for numerically solving differential equations using Euler's method and Newton's method of solving nonlinear equations. 2) It shows an example of using Gaussian elimination to solve a system of linear equations. 3) It demonstrates fitting polynomials of varying degrees to a set of data points using polyfit.

Uploaded by

Daniel Castillo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

CLC Clear H 0.1 y (1) 1 X (1) 0 Xfinal 1.8 I 1 : Function Euler

This document contains code for several mathematical functions and examples: 1) It defines functions for numerically solving differential equations using Euler's method and Newton's method of solving nonlinear equations. 2) It shows an example of using Gaussian elimination to solve a system of linear equations. 3) It demonstrates fitting polynomials of varying degrees to a set of data points using polyfit.

Uploaded by

Daniel Castillo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 Sec clc; clear all format long


i=2; x(2)=8; eps=0.001; error=10;
while abs(error)>eps
x(i+1)=x(i)-(x(i)-x(i-1))/(fonk(x(i))-fonk(x(i-1)))*fonk(x(i));
error=x(i+1)-x(i); i=i+1;
end
x'
function f=fonk(x) f=x^3-9*x^2+2*x+48;

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

A=[2 -1 1 -4; 4 2 -5 22; 5 2 -1 15];


[n,col]=size(A); clc;

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);

clear all; clc;


x=[2 3 4 7 11];
y=[1 6 5 11 9];
plot(x,y,'ro'); hold on;
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:')

clear all; clc;


x=[2 3 5 7 11];
y=[1 6 5 11 9];
plot(x,y,'ro'); hold on;

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:')

You might also like