Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- clc;
- // Instrukcja nr 4
- // krok 1
- a=-2;
- b=0.8;
- r=10;
- n_max=100;
- h=0.001;
- // krok 2
- function y=f(x);
- y=(x^2-3)*sin(x);
- endfunction;
- // krok 3
- function y=fp(x,h);
- y=(f(x+h)-f(x))/h;
- endfunction;
- function y=fpp(x,h);
- y=(fp(x+h,h)-fp(x,h))/h;
- endfunction;
- // krok 4
- n=1;
- x(1)=a;
- if f(a)*f(b)<0 then
- while ~(fp(a,h)*fp(b,h)>0 & fpp(a,h)*fpp(b,h)>0);
- if n>n_max then
- n=n+1;
- x(n)=a+b/2;
- if abs (x(n)-x(n-1))<10^(-r) then
- exit_code=3;
- break;
- else
- if f(x(n))==0 then
- exit_code=4;
- break;
- else
- if f(a)*f(x(n))<0 then
- b=x(n);
- else
- a=x(n);
- end;
- end;
- end;
- else
- exit_code=2
- end;
- end;
- else
- exit_code=1;
- end;
- //przejsice do rf
- if exit_code==0 & n<n_max then
- n=n+1;
- if fp(a,h)*fpp(a,h)<0 then
- c=a;
- x(n)=b;
- else
- c=b;
- x(n)=a;
- end;
- while n<n_max
- n=n+1;
- x(n)=x(n-1)-f(x(n-1))*(c-x(n-1))/(f(c)-f(x(n-1)));
- if abs(x(n)-x(n-1))<10^(-r) then
- exit_code=3;
- break;
- end;
- end;
- if exit_code==0 then
- exit_code=2;
- end;
- else
- exit_code=2;
- end;
- select exit_code
- case 1 then
- disp("niespełnione założenia metody połowienia");
- case 2 then
- disp("przekroczona maksymalna liczba iteracji= "+string(n_max));
- case 3 then
- disp("osiągnięta dokładność rzędu 10^("+string(-r)+")");
- disp("x("+string(n)+")="+string(x(n)));
- case 4 then
- disp("osiągnięto dokładną wartość pierwiastka");
- disp("x("+string(n)+")="+string(x(n)));
- else
- disp("nieznany bląd");
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement