'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 feval
Error: File: funx.m Line: 2 Column: 8
Unexpected MATLAB expression.
Error in pergjysmim (line 12)
fa=feval(fun,a); % llogarit vleren e funksionit ne a

1 Kommentar

Guillaume
Guillaume am 22 Feb. 2018
Is there by any chance another funx function that has higher priority on your path? What does
which -all funx
returns?
What happens if you call funx directly? The problem does not appear to be with feval but with your funx function.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 22 Feb. 2018

0 Stimmen

Error: File: funx.m Line: 2 Column: 8
y=x^4+2*x^3-x-1;
The 8th column is the * . This is a valid Matlab operator and it is not clear, what the problem is. I've tried to copy&paste the character to check, if it is char(42) or any strange Unicode character e.g. copied from a PDF, but it seems to be okay.
Therefore Guillaume's idea could be right: This is not the used file, but another one, which contains a strange character in the mentioned location. If not, delete this character and type it again.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 22 Feb. 2018

Beantwortet:

Jan
am 22 Feb. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by