Текст программы к блок-схеме 2.
function DATA
global a b eps n_plot_dots;
a=2;
b=3;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ x, fx ] = fun_BS_2(a, b, eps)
fa=f(a);
for i=1:100 %цикл со счетчиком ограничивает число итераций
if abs(b-a)>eps % проверка условия продолжения
x=(a+b)/2;
if fa*f(x)<0
b=x;
else
a=x;
end %if
else % вариант требующий прерывания цикла
i % вывод числа выполненных итераций
break;% прерывание цикла
end %if
end %for i
x=(a+b)/2;
fx=f(x);
end % function
function GLAV_BS_2
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_2(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp('_____________________________')
i=0;
for i=1:length(xmas)%подготовка таблицы аргумента и функции
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;%покрытие сеткой
|
|
xlabel('x');%подписи к осям
ylabel('y');
title('Block shem #2');%подпись к графику
end
Блок-схема 3. Метод касательных (Ньютона)
function DATA
global a b eps n_plot_dots;
a=2;
b=3;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)%функция f(x)=0 корни ее ищем
fx=x.^2-5;
end
function [ fpx ] = fp(x)%производная от функции
fpx=2*x;
end
function [ x, fx ] = fun_BS_3(a, b, eps)
h=b-a;
x=a;
for i=1:100
if abs(h)>eps
h=f(x)/fp(x);
x=x-h;
if and(a<x, x<b)% если a<x<b продолжить
else
x=b;
end %if
else % вариант требующий прерывания цикла
i
break;%прерывание цикла
end %if
end %for i
fx=f(x);
end % function
function GLAV_BS_3
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_3(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' __________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #3');
end
Блок-схема 4
Программу к блок-схеме 4 расположим перед блок-схемой.
function DATA
global x dx eps;
x=3;
dx=0.1;
eps=0.0001;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ res ] = fun_BS_4(x, dx, eps)
i=1;
d1=(f(x+dx)-f(x))/dx;
for j=1:100
dx=dx/2;
if i==1
d2=(f(x+dx)-f(x))/dx;
i=2;
else
d1=(f(x+dx)-f(x))/dx;
i=1;
end %if
if abs(d1-d2)>eps
else
j
break;
end %if
end %for j
if i==1
res=d1;
else
res=d2;
end %if
end % function
function GLAV_BS_4
global x dx eps res;
DATA;
[ res ] = fun_BS_4(x, dx, eps);
REPORT;
end
function REPORT
global x dx eps res;
disp('Block scheme #4');
disp('Arguments');
disp(['x = ' num2str(x,'%10.5f') ]);%
disp(['dx = ' num2str(dx,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Result');
disp(['res = ' num2str(res,'%10.5f') ]);
|
|
end
Блок-схема 5
function DATA
global a b eps n_plot_dots;
a=1;
b=3;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ x, fx ] = fun_BS_5(a, b, eps)
x=a;
f1=f(a);
h=b-a;
for i=1:100
if abs(h)>eps
h=h/3;
for j=1:100
x=x+h;
f2=f(x);
if f1*f2>0
else
j
break;
end %if
end %for j
x=x-h;
else
i
break;
end %if
end %for
x=x-h/2;
fx=f(x);
end % function
function GLAV_BS_5
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_5(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #5');
end
Блок-схема 6
function DATA
global a b eps n_plot_dots;
a=1;
b=3;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ x, fx ] = fun_BS_6(a, b, eps)
h=(b-a)/2;
x=a;
fa=f(x);
for i=1:100
if abs(h)>eps
x=x+h;
fx=f(x);
if fa*fx<0
x=x-h;
else
fa=fx;
end %if
h=h/2;
else
i
break;
end %if
end %for i
fx=f(x);
end % function
function GLAV_BS_6
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_6(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #6');
end
Блок-схема 7
function DATA
global a b eps n_plot_dots;
a=-1;
b=2;
eps=0.0001;
n_plot_dots=101;
end
function [ x, fx ] = fun_BS_7(a, b, eps)
x=a;
f1=f(x);
h=(b-a)/3;
for i=1:100
if abs(h)>eps
x=x+h;
if x>b
x=b;
end %if
f2=f(x);
if f2>f1
h=-h/3;
end %if
f1=f2;
else
i
break;
end %if
end % for i
x=x+2*h;
fx=f(x);
end % function
function [ fx ] = f(x)
fx=x.^2-5;
end
function GLAV_BS_7
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_7(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #7');
end
Блок-схема 8
function DATA
global a b eps n_plot_dots;
a=1;
b=3;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ x, fx ] = fun_BS_8(a, b, eps)
n=log((b-a)/eps)/log(2);
fa=f(a);
i=0;
for j=1:100
if i<n
x=(a+b)/2;
fx=f(x);
if fa*fx<0
b=x;
else
a=x;
end %if
i=i+1;
else
j
break;
end %if
end %for
fx=f(x);
end % function
function GLAV_BS_8
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_8(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #8');
end
Блок-схема 9
function DATA
global x0 y0 xk n eps;
x0=1;
y0=1;
xk=2;
eps=0.01;
n=10;
end
function [ fxy ] = f(x,y)
fxy=x*y;
end
function [ xmas, fmas, Jres ] = fun_BS_9(x0,y0,xk,n, eps)
yk=0;
y=2*eps;
for i=1:100
if abs(yk-y)>eps
h=(xk-x0)/n;
yk=y;
x=x0;
y=y0;
n=n*2;
for j=1:100
if x<xk
y=y+f(x,y)*h;
x=x+h;
xmas(j)=x;
fmas(j)=y;
Jres=j;
else
j
|
|
break;
end %if
end %for j
n=n*2;
else
i
break;
end %if
end % for i
disp ('OK BS 9');
end % function
function GLAV_BS_9
global x0 y0 xk n xmas fmas Jres eps;
DATA;
[ xmas, fmas, Jres ] = fun_BS_9(x0,y0,xk,n,eps);
REPORT;
end
function REPORT
global x0 y0 xk n eps xmas fmas Jres;
disp('Arguments');
disp(['x0 = ' num2str(x0,'%10.5f') ]);
disp(['xk = ' num2str(xk,'%10.5f') ]);
disp(['y0 = ' num2str(y0,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['Jres = ' num2str(Jres,'%10.5f') ]);
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:Jres
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.5f\t%10.5f\t %10.5f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #9');
end
Блок-схема 10
function DATA
global a b eps n_plot_dots;
a=-1;
b=2;
eps=0.0001;
n_plot_dots=101;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ x, fx ] = fun_BS_10(a, b, eps)
x1=a;
x4=b;
Z=1/3;
for i=1:100
x2=x1+Z*(x4-x1);
x3=x4-Z*(x4-x1);
F2=f(x2);
F3=f(x3);
if F2<F3
x4=x3;
else
x1=x2;
end %if
if abs(x4-x1)>2*eps
else
i
break;
end %if
end %for i
x=(x1+x4)/2;
fx=f(x);
end % function
function GLAV_BS_10
global a b eps x fx n_plot_dots;
DATA;
[ x, fx ] = fun_BS_10(a, b, eps);
REPORT;
end
function REPORT
global a b eps x fx n_plot_dots;
xmas=zeros(n_plot_dots);
fmas=zeros(n_plot_dots);
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp('Results');
disp(['x = ' num2str(x,'%10.5f') ]);
disp(['fx = ' num2str(fx,'%10.5f') ]);
h=(b-a)/(n_plot_dots-1);
for i=1:n_plot_dots
if i==1
xmas(i)=a;
else
xmas(i)=xmas(i-1)+h;
end %if
fmas(i)=f(xmas(i));
end
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:length(xmas)
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.3f\t%10.3f\t %10.3f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
grid on;
xlabel('x');
ylabel('y');
title('Block shem #10');
end
Блок-схема 11
function DATA
global x0 y0 xk n;
x0=0;
y0=1;
xk=1;
n=10;
end
function [ fxy ] = f(x,y)
fxy=x*y;
end
function [ xmas, fmas, Jres ] = fun_BS_11(x0,y0,xk,n)
h=(xk-x0)/n;
x=x0;
y=y0;
for i=1:100
if x<xk
f0=f(x,y);
x1=x+h/2;
y1=y+f0*h/2;
y=y+f(x1,y1)*h;
x=x+h;
xmas(i)=x;
fmas(i)=y;
Jres=i;
else
i
break;
end %if
end %for i
disp('OK BS 11');
end % function
function GLAV_BS_11
global x0 y0 xk n xmas fmas Jres;
DATA;
[ xmas, fmas, Jres ] = fun_BS_11(x0,y0,xk,n);
REPORT;
end
function REPORT
global x0 y0 xk n xmas fmas Jres;
disp('Arguments');
disp(['x0 = ' num2str(x0,'%10.5f') ]);
disp(['xk = ' num2str(xk,'%10.5f') ]);
disp(['y0 = ' num2str(y0,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(' i x fx ');
disp(' ________________________________')
i=0;
for i=1:Jres
xx=xmas(i);
ffx=fmas(i);
disp(sprintf('%10.5f\t%10.5f\t %10.5f',i,xx,ffx));
end %for
plot(xmas,fmas,'r.');
|
|
grid on;
xlabel('x');
ylabel('y');
title('Block shem #11');
end
Блок-схема 12
function DATA
global a b eps n;
a=1;
b=2;
eps=0.0001;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s ] = fun_BS_12(a, b, n, eps)
s1=0;
s=2*eps;
for i=1:100
if abs(s-s1)>eps
s1=s;
h=(b-a)/n;
x=a+h/2;
s=0;
for j=1:100
if x<b
s=s+f(x);
x=x+h;
else
j
break;
end %if
end %for j
s=s*h;
n=n*2;
else
i
break;
end %if
end %for i
end % function
function GLAV_BS_12
global a b eps s n;
DATA;
[ s ] = fun_BS_12(a, b, n, eps)
REPORT;
end
function REPORT
global a b eps s n;
disp('BS 12');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['eps = ' num2str(eps,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
end
Блок-схема 13
function DATA
global a b n;
a=1;
b=2;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s, s1 ] = fun_BS_13(a, b, n)
h=(b-a)/n;
s=0;
i=1;
for i=1:100
if i<=n
x=a+(i-1)*h;
s=s+f(x);
i=i+1;
else
i
break;
end %if
end %for i
s1=0;
for x=a:h:b-h
s1=s1+f(x);
end %for i
s=s*h;
s1=s1*h;
end % function
function GLAV_BS_13
global a b s s1 n;
DATA;
[ s, s1 ] = fun_BS_13(a, b, n);
REPORT;
end
function REPORT
global a b s s1 n;
disp('BS 13');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
disp(['s1 = ' num2str(s1,'%10.5f') ]);
end
Блок-схема 14
function DATA
global a b n;
a=1;
b=2;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s, s1 ] = fun_BS_14(a, b, n)
h=(b-a)/n;
s=0;
x=a+h/2;
for i=1:100
s=s+f(x);
x=x+h;
if x<b
else
i
break;
end %if
end %for i
s1=0;
for x=a+h/2:h:b
s1=s1+f(x);
end %for i
s=s*h;
s1=s1*h;
end % function
function GLAV_BS_14
global a b s s1 n;
DATA;
[ s, s1 ] = fun_BS_14(a, b, n);
REPORT;
end
function REPORT
global a b s s1 n;
disp('BS 14');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
disp(['s1 = ' num2str(s1,'%10.5f') ]);
end
Блок-схема 15
function DATA
global a b n;
a=1;
b=2;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s, s1 ] = fun_BS_15(a, b, n)
h=(b-a)/n;
s=0;
x=a+h/2;
for i=1:100
if x<b
s=s+f(x);
x=x+h;
else
i
break;
end %if
end %for i
s1=0;
for x=a+h/2:h:b
s1=s1+f(x);
end%for i
s=s*h;
s1=s1*h;
end % function
function GLAV_BS_15
global a b s s1 n;
DATA;
[ s, s1 ] = fun_BS_15(a, b, n);
REPORT;
end
function REPORT
global a b s s1 n;
disp('BS 15');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
disp(['s1 = ' num2str(s1,'%10.5f') ]);
end
Блок-схема 16
function DATA
global a b n;
a=1;
b=2;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s, s1 ] = fun_BS_16(a, b, n)
h=(b-a)/n;
s=0;
i=0;
for j=1:100
x=a+i*h;
s=s+(f(x)+f(x+h))/2;
i=i+1;
if i<=n-1
else
j
break;
end %if
end % for j
s1=0;
for i=0:n-1
x=a+i*h;
s1=s1+(f(x)+f(x+h))/2;
end % for i
s=s*h;
s1=s1*h;
end % function
function GLAV_BS_16
global a b s s1 n;
DATA;
[ s, s1 ] = fun_BS_16(a, b, n);
REPORT;
end
function REPORT
global a b s s1 n;
disp('BS 16');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
disp(['s1 = ' num2str(s1,'%10.5f') ]);
end
Блок-схема 17
function DATA
global a b n;
a=1;
b=2;
n=10;
end
function [ fx ] = f(x)
fx=x.^2-5;
end
function [ s, s1 ] = fun_BS_17(a, b, n)
h=(b-a)/n;
x=a;
s=0;
i=2;
for j=1:100
s=s+f(x)+4*f(x+h)+f(x+2*h);
x=x+2*h;
i=i+2;
if i<=n
else
j
break;
end %if
end %for j
x=a;
s1=0;
for i=0:n-1
s1=s1+f(x)+4*f(x+h)+f(x+2*h);
x=x+2*h;
end %for i
s=h*s/3;
s1=s1*h/3;
end % function
function GLAV_BS_17
global a b s s1 n;
DATA;
[ s, s1 ] = fun_BS_17(a, b, n);
REPORT;
end
function REPORT
global a b s s1 n;
disp('BS 17');
disp('Arguments');
disp(['a = ' num2str(a,'%10.5f') ]);
disp(['b = ' num2str(b,'%10.5f') ]);
disp(['n = ' num2str(n,'%10.5f') ]);
disp('Results');
disp(['s = ' num2str(s,'%10.5f') ]);
disp(['s1 = ' num2str(s1,'%10.5f') ]);
end
Погрешности
Основная задача теории погрешностей состоит в оценке погрешности результата вычислений при известных погрешностях исходных данных.