Você está na página 1de 7

212022018

clear all
for n=1:2
f=inline('x^4-7*x^3+21*x^2-8*x-7');
x1=0;x3=0;
if n==2
x2=-1;
else
x2=3;
end
while abs(f(x3))>10^-4
x3=(x1+x2)/2;
if f(x3)*f(x1)<0
x2=x3;
else
x1=x3;
end
end
fprintf('%s %d %s %.3f\n','
root',n,'=',round(x3*1000)/1000)
end
Results:
root 1 = 1.000
root 2 = -0.399

212022018
clear all
format long
f=inline('exp(-x^2)-log(x)');
syms x
x1=1;x0=2;
g=inline(diff(f(x)));
while abs(x0-x1)>10^-6
x0=x1;
x1=x0-f(x0)/g(x0);
end
fprintf('%s %.6f\n','root=',round(x1*10^6)*10^-6)

Results:
root= 1.239839

212022018
clear all
format long
f=inline('exp(-x^2)-log(x)');
x1=1;x2=2;
while abs(x2-x1)>10^-6
x0=x1;
x1=x2;
x2=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));
end
fprintf('%s %.6f\n','root=',round(x2*10^6)*10^-6)

Result:
root= 1.239839

212022018
clear all
format long
f=inline('x^3-2*x-5');
x1=1;x2=3;
while abs(x2-x1)>10^-6
if f(x1)*f(x2)<0
x0=x2;
else
x1=x2;
end
x2=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));
end
fprintf('%s %.6f\n','root=',round(x2*10^6)*10^-6)

Result:
root= 2.094551

212022018
clear all
for n=1:2
f=inline('2^x-5*x+2');
x1=0;x3=0;
if n==2
x2=4;
x1=5;
else
x2=1;
end
while abs(f(x3))>10^-4
x3=(x1+x2)/2;
if f(x3)*f(x1)<0
x2=x3;
else
x1=x3;
end
end
fprintf('%s %d %s %.3f\n','
root',n,'=',round(x3*1000)/1000)
end

Results:
root 1 = 0.732
root 2 = 4.277

212022018
clear all
f=inline('atan((1+x)/2)');
x0=4; x1=0;
while abs(x0-x1)>10^-4
x0=x1;
x1=f(x0);
end
fprintf('%s %.4f\n','Root=',round(x1*10000)/10000)

Result:
Root= 0.7063

212022018
clear all
f=inline('(1/2*(1+2*sin(x)))');
x0=4; x1=0;
while abs(x0-x1)>10^-4
x0=x1;
x1=f(x0);
end
fprintf('%s %.4f\n','Root=',round(x1*10000)/10000)

Result:
Root= 1.4973

Você também pode gostar