'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 Commento

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.

Accedi per commentare.

 Risposta accettata

Jan
Jan il 22 Feb 2018

0 voti

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.

Più risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Tag

Richiesto:

il 22 Feb 2018

Risposto:

Jan
il 22 Feb 2018

Community Treasure Hunt

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

Start Hunting!

Translated by