MATLAB Answers

Error using feval Function to evaluate must be represented as a string scalar, character vector, or function_handle object.

조회 수: 72(최근 30일)
Eli Shelton
Eli Shelton 2021년 5월 6일
답변: Sindhu Karri 2021년 5월 13일
my code is:
function vib = explicitwave(a,b,h,k,fic,gic,tm)
x = a:h:b;
nt = ceil(tm/k);
nx = ceil((b-a)/h);
nxm1 = nx - 1;
r = k/h;
r2 = r*r;
uinitial = feval(fic,x(2:nx));
u0 = [0 uinitial 0]';
u1(1) = 0
g = feval(gic,x);
for i = 2:nxm1
u1(i) = r/2*u0(i-1) + (1-r2)*u0(i) + r2/2*u0(i+1) - k*g(i);
end
u1(nx+1) = 0
vib = [u0 u1'];
Uoldest = u0;
Uold = u1';
for j = 3:nt
Unew(1) = 0;
for i = 2:nx
Unew(i) = r2*Uold(i-1) + 2*(1-r2)*Uold(i) + r2*Uold(i+1) - Uoldest(i);
end
Unew(nx+1) = 0;
vib = [vib Unew'];
Uoldest = Uold;
Uold = Unew;
end
time = 0:k:tmax;
titlestr = ['Wave Mesh Plot, times 0:tfinal = ' num2str(tm) ', step k = ' num2str(k)];
mesh(time, x, vib); title(titlestr); xlabel('Time'); ylabel('Position'); zlabel('Wave Values'); shg
end
my initial condition mfiles are:
gic.m
function initial = fic(x)
initial = 2*sin(pi*x);
end
and
fic.m
function initial = gic(x)
initial = -sin(2*pi*x);
end
Then I set a=0, b=1, tm=1, h=0.1, k=0.1, x = a:h:b, fic=fic(x), gic=gic(x)
Then when I try to run
explicitwave(a,b,h,k,fic,gic,tm)
I get the error:
Error using feval
Function to evaluate must be represented as a string
scalar, character vector, or function_handle object.
Error in explicitwave (line 10)
uinitial = feval(fic,x);
Can someone help me fix this problem?

답변(1개)

Sindhu Karri
Sindhu Karri 2021년 5월 13일
Hii,
Here the error is because of the feval function.You need to pass a function handle or function name as an input to feval .
Call the function as
explicitwave(a,b,h,k,@fic,@gic,tm)
without assigning fic=fic(x) and gic=gic(x).
And also tmax variable is not defined in the attached code
Refer to below link more understanding on feval function

Community Treasure Hunt

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

Start Hunting!

Translated by