ЧМ_2
.docx#1
function [ ] = f(g)
x = -10:0.5:10;
g=x.^3-3.*x.^2-9.*x-5;
plot(x,g);
p = [1 -3 -9 -5];
r = roots(p);
axis square
hold on
grid on
line([-10 10],[0 0])
line([0 0],[-1500 1500])
end
#2
function dio (f,a,b,e)
syms l;
for i=0:1:100
if ((b-a)/2^i> e)
l=(a+b)/2;
if ((subs(f,l))<=0)
a=l;
end
if ((subs(f,l))>=0)
b=l;
end
else
i
l %корень
break
end
end
end
f=@(y) (y.^3-3*y.^2-9*y-5);
dio (f,2,6,0.001)
i = 2
l = 5
#3
function new(f,a,b,eps)
syms e x y;
e=abs(a-b);
x=b;
d=diff(f,y,1);
for i=0:1:100
if (abs(e)>eps)
fn=subs(f,x);
dn=subs(d,x);
x=x-fn/dn;
e=abs(x-b);
b=x;
else
break
end
end
i
x
end
f=@(x)x^3-3*x^2-9*x-5;
new(f,2,9,0.01)
i = 5
l = 62459170824861085845965911/12491830475623402427282859
#4
function newmod(f,a,b,eps,p)
syms e x y;
e=abs(a-b);
x=b;
d=diff(f,y,1);
for i=0:100
if (abs(e)>eps)
fn=subs(f,x);
dn=subs(d,x);
x=x-p*fn/dn;
e=abs(x-b);
b=x;
else
break
end
end
i
x
>>newmod(f,-3,-0.5,0.01,2)
i = 3
l = -5441277115/5441277114
#5
function x1=mpi(a,eps)
x1=3;
x2=0;
for i=0:1:100
if abs(x1-x2)<=eps;
disp('Количество итераций')
i
break
else
x2=x1;
x1=(1/2)*(a/x2+x2);
end
end
disp('Корень уравнения')
end
>>mpi(256,0.0001)
Количество итераций
i = 7
Корень уравнения
ans = 16
#6
function x1=q(a,c,d)
syms k;
x1=c+0.1;
x2=d-0.3;
k=0;
if abs((x1^2+x1-a)-(x2^2+x2-a))<=0.3*abs((x1-x2));
k=1;
k
else
k=0;
k
end
end
>> q(16, 3,6)
k =0
#7
>> f=@(x)x^3-3*x^2-9*x-5;
>> fzero(f,3)
ans =5
>> fzero(f,4)
ans =5
>> fzero(f,-1)
ans =-1
#8
function x1=mpi5(x1,eps)
x2=0;
for i=0:1:100
if abs(x1-x2)<=eps;
disp('Количество итераций')
i
break
else
x2=x1;
x1=2*sin(x2);
end
end
disp('Корень уравнения')
end
mpi5(-2,0.001)
Количество итераций
i =
13
Корень уравнения
ans =
-1.8952
mpi5(2,0.001)
Количество итераций
i =
13
Корень уравнения
ans =
1.8952
>> f=@(x) sin(x)-x/2;
>> del (f,-1,2, 0.00001)
i =
10
c =
-9.7656e-04
>> f=@(x) sin(x)-x/2;
>> fzero(f,-4)
ans =
-1.8955
>> fzero(f,-0.5)
ans =
3.9443e-30
>> fzero(f,1)
ans =
1.8955