'pergjysmim.m'
function [alfa,k]=pergjysmim(fun,a,b,tol,max_iter) % funksioni [alfa,k]=pergjysmim(fun,a,b,tol,max_iter)
% fun eshte variabel string per emrin e funksionit
% a dhe b jane skajet e segmentit qe permban rrenjen
% tol eshte gabimi (toleranca) i lejuar per rrenjen e gjetur
% max_iter eshte numri maksimal i iteracioneve te lejuara
% alfa eshte perafrimi i gjetur i rrenjes me gabim
% me te vogel se tol
% k numeron iteracionet e kryera
fa=feval(fun,a); fb=feval(fun,b); if sign(fa)*sign(fb)>0 error('Funksioni fun nuk ndryshon shenje ne [a,b]'); end k=1; while k<=max_iter c=a+(b-a)/2; % kjo eshte me mire se c=(a+b)/2;
if abs(b-a)/2<tol alfa=c; return; end k=k+1; fc=feval(fun,c); if sign(fa)*sign(fc)>0 a=c; fa=fc; elseif sign(fa)*sign(fc)<0 b=c; fb=fc; else disp('rrenja eshte gjetur me saktesine e precizionit te kompjuterit') alfa=c; return; end end error('Eshte tejkaluar numri maksimal i iteracioneve te lejuara-programi pergjysmim ka mbaruar egzekutimin')
'funx.m'
function y=funx(x) y=x^4+2*x^3-x-1;end
Command Window
>>[zgj,iter]=pergjysmim('funx',0,1,5e-07,100)Error using fevalError: File: funx.m Line: 2 Column: 8Unexpected MATLAB expression.Error in pergjysmim (line 12)fa=feval(fun,a); % llogarit vleren e funksionit ne a
Best Answer