Error using feval and Invalid function name
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Víctor Sánchez
am 16 Jan. 2022
Kommentiert: Munna
am 7 Jan. 2025
Please friends, i will aprecciate your help,
I have this code:
function I= Trapecios(f,a,b,n)
%Fórmula Trapecios compuesta
h=(b-a)/n;
x=a:h:b;
x=x(:);
for i=1:n-1
k1=feval(f,x(i));
pesos=[1 2*ones(1,n-1) 1];
I=h./2*sum(pesos*k1);
end
end
So when I input the arguments, issues these messagges:
>> Trapecios('x/56',-56/2,9*56/10,8)
Error using feval
Invalid function name 'x/56'.
Error in Trapecios (line 7)
k1=feval(f,x(i));
2 Kommentare
Munna
am 7 Jan. 2025
Error in RCGAF (line 56)
ObjVal(i)=feval(objfun, chromosomes(i,:)); this problem come during running my slx please solve
Akzeptierte Antwort
Voss
am 16 Jan. 2022
You can use an anonymous function, e.g., f = @(x)x/56, or a named function, rather than a character array (f = 'x/56') for feval.
Trapecios(@(x)x/56,-56/2,9*56/10,8) % using anonymous function @(x)x/56
Trapecios(@divide_by_56,-56/2,9*56/10,8) % using a handle to a named function, defined below
Trapecios('divide_by_56',-56/2,9*56/10,8) % using the name of the function
function out = divide_by_56(x)
out = x/56;
end
function I= Trapecios(f,a,b,n)
%Fórmula Trapecios compuesta
h=(b-a)/n;
x=a:h:b;
x=x(:);
for i=1:n-1
k1=feval(f,x(i));
pesos=[1 2*ones(1,n-1) 1];
I=h./2*sum(pesos*k1);
end
end
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!