- 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).
Undefined function 'minus' for input arguments of type 'function_handle'. and no output
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Rik
am 11 Mär. 2020
Apart from not copying the full error message (which would have contained the trace of offending lines, there are more issues.
Antworten (1)
tmarske
am 11 Mär. 2020
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 Kommentare
Rik
am 11 Mär. 2020
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?
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!