Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- clc;
- a=0.5;
- b=2;
- r=4;
- n_max=50;
- h=0.001;
- exit_code=0;
- function y=f(x);
- y=(x^2-3)*sin(x);
- endfunction;
- 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;
- 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))
- //metoda połowienia
- 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;
- //metoda reguly falsi
- // zmienić na metodę siecznych
- if (exit_code==0 & n<n_max) then
- n=n+1;
- //to zmienić żeby liczyło n+2 i n+1
- if fp(a,h)*fpp(a,h)<0 then
- x(n)=b;
- n=n+1;
- x(n)=a;
- else
- x(n)=a;
- n=n+1;
- x(n)=b;
- end;
- while (n<n_max)
- n=n+1;
- //tu zmienic żeby używało n+2 i n+1
- x(n)=x(n-1)-f(x(n-1))*(x(n-1)-x(n-2))/(f(x(n-1))-f(x(n-2)));
- 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;
- else
- exit_code=1;
- 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ęto 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("Wystąpił błąd");
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement