Error using feval and Invalid function name

조회 수: 5 (최근 30일)
Víctor Sánchez
Víctor Sánchez 2022년 1월 16일
댓글: Munna 2025년 1월 7일
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
Munna
Munna 2025년 1월 7일
Error in RCGAF (line 56)
ObjVal(i)=feval(objfun, chromosomes(i,:)); this problem come during running my slx please solve
Munna
Munna 2025년 1월 7일
what problem in objVal funtion

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 1월 16일
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
ans = 43.1200
Trapecios(@divide_by_56,-56/2,9*56/10,8) % using a handle to a named function, defined below
ans = 43.1200
Trapecios('divide_by_56',-56/2,9*56/10,8) % using the name of the function
ans = 43.1200
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
Víctor Sánchez
Víctor Sánchez 2022년 1월 16일
It worked bro!!!!... Thank you thank you a lot. !!!
Voss
Voss 2022년 1월 16일
No problem!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by