Undefined function 'minus' for input arguments of type 'function_handle'. and no output

3 次查看(过去 30 天)
function [t,u]=fixeddiffusion(h,dt,t0,theta,u0,uN,uinitial,x0,xN,uexact)
N=(xN-x0)/h;
x=zeros(N+1,1);
x(1)=x0;
x(N+1)=xN;
uvec=zeros(N+1,1);
uvec(1)=u0;
uvec(N+1)=uN;
A=zeros(N-1,N-1);
a=-2/(h^2);
for i=1:N-1
A(i,i)=a;
end
b=(1/(h^2));
for i=1:N-2
j=i+1;
A(i,j)=b;
end
for i=2:N-1
k=i-1;
A(i,k)=b;
end
b=zeros(N-1,1);
for i=1:N-1
b(i)=uinitial(i+1);
end
uu=A\b;
for i=1:N-1
uu(i)=uvec(i+1);
end
u=zeros(N+1,1);
u(1)=u0;
for i=2:N
u(i)=u(i-1)+dt*((1-theta)*uvec(i-1)+theta*uvec(i));
end
t=zeros(N+1);
for i=1:N+1
t(i)=t0+(i-1)*dt;
end
ureal=zeros(N+1,1);
for i=1:N+1
ureal(i)=uexact(x(i),t(i));
end
figure(1)
plot(t,u,'.',t,ureal,'--');
legend('Approx:','True:');
error=zeros(N+1,1);
error=abs(u-ureal);
norm=zeros(N+1,1);
norm(i)=symsum((h*dt(abs(error(i)^2)))^(1/2),error(i),1,i);
end
When I run the following:
uinitial=@(x)sin(pi*x);
u0=0;
uN=0;
h=0.1;
dt=0.0005;
theta=0;
x0=0;
xN=1;
uexact=@(x,t)exp((-pi^2)*t)*sin(pi*x);
[t,u]=fixeddiffusion(h,dt,theta,u0,uN,uinitial,x0,xN,uexact)
It comes up with the error message. It also says "Conversion to double from function_handle is not possible."
  1 个评论
Rik
Rik 2020-3-11
Apart from not copying the full error message (which would have contained the trace of offending lines, there are more issues.
  • You used error as a variable name, wich will now shadow the builtin function, so now you can't trigger errors with a description.
  • You did not auto-indent your code, which reduces readability.
  • You did not use very descriptive variable names. Short names are quick to type, but provide no clues about their meaning.
  • You did not write any comments explaining your code, or a function header explaining the goal of the function and syntax options.
  • You did not do basic input checking (nargin and nargout are very helpful if you want a very fast check if all your inputs and outputs are as you expect).

请先登录,再进行评论。

回答(1 个)

tmarske
tmarske 2020-3-11
It would be helpful if you indicated where exactly the error occurred the Matlab error message should give you a line number. A long wall of text like this is not easy to parse.
However I believe the error here is because you missed the 't0' argument in your call to fixeddiffusion():
[t,u]=fixeddiffusion(h,dt,theta,u0,uN,uinitial,x0,xN,uexact)
cf the function definition:
function [t,u]=fixeddiffusion(h,dt,t0,theta,u0,uN,uinitial,x0,xN,uexact)
  2 个评论
Phoebe Tyson
Phoebe Tyson 2020-3-11
Here is the full error message after including t0:
Array indices must be positive integers or logical values.
Error in fixeddiffusion (line 50)
norm(i)=symsum((h*dt(abs(error(i)^2)))^(1/2),error(i),1,i);
Error in CW1q2 (line 12)
[t,u]=fixeddiffusion(h,dt,t0,theta,u0,uN,uinitial,x0,xN,uexact)
Rik
Rik 2020-3-11
You still haven't fixed the issues I pointed out in my comment. Also, what do you think this error means? Where in this line of code are you indexing an array? What is the value of that index?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by