Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Uses GraphABC;
- function func(x: double): double;
- begin
- Result:=(sin(x)/cos(x) - (power(sin(x)/cos(x), 3) / 3) + (power(sin(x)/cos(x), 5) / 5) - (1.0 / 3));
- end;
- var counter:array of integer;
- e: array of double;
- N, w, h, height, length, i, mastx, masty: integer;
- a, b, x, fa, fb, fx, e1, maxcounter: double;
- conv: string;
- begin
- w:=1900; h:=1000; length:= w - 50; height:= h - 100; a:= 0; b:= 0.8;
- setwindowwidth(w);
- setwindowheight(h);
- fa:=func(a); fb:=func(b);
- writeln('Нахождение корня уравнения tg(x)-(tg(x)^3)/3+(tg(x)^5)/5-1/3=0 методом половинного деления на отрезке [0;0.8].');
- if(fa*fb>0) then
- writeln('На заданном отрезке нет корня.')
- else
- begin
- writeln('Введите, при скольких различных точностях вы хотите решить данное уравнение (целое число):');
- read(N);
- writeln('N = ', N);
- writeln('Введите начальное значение точности (действительное число). Дальнейшие точности будут в 2 раза меньше предыдущей:');
- read(e1);
- writeln('E = ', e1);
- SetLength(counter,n); SetLength(e,n);
- for i:=0 to n-1 do
- begin
- a:= 0; b:= 0.8;
- counter[i]:=0;
- if i=0 then
- e[i]:=e1
- else
- begin
- e[i]:=e[i-1]/2;
- e1:=e[i];
- end;
- while(true) do
- begin
- x:=(b + a) / 2;
- fx:=func(x);
- if fx*fa<0 then
- begin
- b:= x;
- fb:= fx;
- end
- else
- begin
- a:= x;
- fa:= fx;
- end;
- counter[i]:=counter[i]+1;
- if(abs(b-a)<=e1) then
- begin
- x:=(b + a) / 2;
- counter[i]:=counter[i]+1;
- writeln('Ответ: x=',x);
- writeln('Количество итераций для точности e=', e[i],': ', counter[i]);
- break;
- end;
- end;
- end;
- mastx:= round(length/n);
- maxcounter:=counter[0];
- for i:=1 to n-1 do
- if counter[i]>maxcounter then
- maxcounter:=counter[i];
- masty:=round(height/maxcounter);
- writeln('Нажмите Enter, чтобы вывести диаграмму сравнения количества итераций к точности.');
- readln();
- readln();
- ClearWindow;
- line(30, 30, 30, 30 + height);
- line(30, 30, 25, 40);
- line(30, 30, 35, 40);
- line(30, 30 + height, 20 + length, 30 + height);
- line(20 + length, 30 + height, length + 15, 25 + height);
- line(20 + length, 30 + height, length + 15, 35 + height);
- textout(length + 20, 45 + height, 'i');
- textout(length + 20, 60 + height, 'e');
- textout(40, 30, 'Counter');
- for i:=0 to n-1 do
- begin
- line(30 + i * mastx, 20 + height, 30 + i * mastx, 40 + height);
- conv:=inttostr(i);
- textout(30 + i * mastx, 45 + height, conv);
- conv:=floattostr(e[i]);
- textout(30 + i * mastx, 60 + height, conv);
- line(20, 30 + height - counter[i] * masty, 40, 30 + height - counter[i] * masty);
- conv:=inttostr(counter[i]);
- textout(5, 30 + height - counter[i] * masty, conv);
- end;
- for i:=0 to n-1 do
- begin
- line(30 + i * mastx, 30 + height, 30 + i * mastx, 30 + height - counter[i] * masty);
- line(30 + i * mastx, 30 + height - counter[i] * masty, 30 + (i + 1) * mastx, 30 + height - counter[i] * masty);
- line(30 + (i + 1) * mastx, 30 + height, 30 + (i + 1) * mastx, 30 + height - counter[i] * masty);
- end;
- readln();
- ClearWindow;
- writeln('Нажмите Enter, чтобы вывести график сравнения количества итераций к точности.');
- readln();
- ClearWindow;
- line(30, 30, 30, 30 + height);
- line(30, 30, 25, 40);
- line(30, 30, 35, 40);
- line(30, 30 + height, 20 + length, 30 + height);
- line(20 + length, 30 + height, length + 15, 25 + height);
- line(20 + length, 30 + height, length + 15, 35 + height);
- textout(length + 20, 45 + height, 'i');
- textout(length + 20, 60 + height, 'e');
- textout(40, 30, 'Counter');
- for i:=0 to n-1 do
- begin
- line(30 + i * mastx, 20 + height, 30 + i * mastx, 40 + height);
- conv:=inttostr(i);
- textout(30 + i * mastx, 45 + height, conv);
- conv:=floattostr(e[i]);
- textout(30 + i * mastx, 60 + height, conv);
- line(20, 30 + height - counter[i] * masty, 40, 30 + height - counter[i] * masty);
- conv:=inttostr(counter[i]);
- textout(5, 30 + height - counter[i] * masty, conv);
- end;
- for i:=0 to n-2 do
- begin
- line(30 + i * mastx, 30 + height - counter[i] * masty, 30 + (i + 1) * mastx, 30 + height - counter[i + 1] * masty);
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement