Invalid use of Operator error

3 次查看(过去 30 天)
I've written a simple script (from a textbook that too) but it doesn't run thanks to this error (Matlab Online R2020A). How do I fix the error and run the script?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1D transient heat conduction %
% space discretization: finite- %
% difference time integration: %
% Explicit Euler %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-- get initial wall time:
time0=clock();
%--- Simulation cell parameters:
Nx=128;
dx = 1.0;
%--- Time integration parameters:
nstep = 600; % no of integration steps
nprint = 200; % print frequency to output
dtime = 0.2;
%-- Initialize temperature field &
%grid:
for i=1:Nx
u0(i) = 0.0; % initial temp
x(i)= i*dx; % initialise 1d space grid
if((i >= 44) && (i <= 84))
u0(i)=1.0;
end
end
%--
%-- Evolve temperature field
%--
ncount=0;
for istep= 1:nstep % time summation
for i=2:Nx-1 % giving temp values at this time
u0(i) = u0(i) + dtime*1*(u0(i+1)-2.0*u0 . . .
(i)+u0(i-1)) . . .
/(dx*dx); % adding increment term!
end
%-- Display results:
if((mod(istep,nprint) == 0) ||(istep
== 1))
ncount=ncount+1;
subplot(2,2,ncount);
plot(x,u0);
time=sprintf('%d',istep);
title(['time' time]);
axis([0 Nx -0.5 1.5]);
xlabel('x');
ylabel('Temperature');
end %if
end %istep
compute_time=etime(clock(),time0); % for program efficiency!
fprintf('Compute Time: %5d\n',
compute_time);

采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-3-24
Hi Pranav,
Direct copy paste may not place it exactly as how is it written in the book. You could try to make slight modifications to make it work.
Here is the modified code that works with annotations as why it didnt go correct initially:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1D transient heat conduction %
% space discretization: finite- %
% difference time integration: %
% Explicit Euler %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-- get initial wall time:
time0=clock();
%--- Simulation cell parameters:
Nx=128;
dx = 1.0;
%--- Time integration parameters:
nstep = 600; % no of integration steps
nprint = 200; % print frequency to output
dtime = 0.2;
%-- Initialize temperature field &
%grid:
for i=1:Nx
u0(i) = 0.0; % initial temp
x(i)= i*dx; % initialise 1d space grid
if((i >= 44) && (i <= 84))
u0(i)=1.0;
end
end
%--
%-- Evolve temperature field
%--
ncount=0;
for istep= 1:nstep % time summation
for i=2:Nx-1 % giving temp values at this time
u0(i) = u0(i) + dtime*1*(u0(i+1)-2.0*u0 ... %% Here there shouldn't be space between the dots, they should be contiguous
(i)+u0(i-1)) ... %% Same here as above
/(dx*dx); % adding increment term!
end
%-- Display results:
if((mod(istep,nprint) == 0) ||(istep== 1)) %% The condition is placed in two lines, which must be in single line, or use ...
ncount=ncount+1;
subplot(2,2,ncount);
plot(x,u0);
time=sprintf('%d',istep);
title(['time' time]);
axis([0 Nx -0.5 1.5]);
xlabel('x');
ylabel('Temperature');
end %if
end %istep
compute_time=etime(clock(),time0); % for program efficiency!
fprintf('Compute Time: %5d\n',... %% The second argument is in two lines, which should be in single line or with usage of ...
compute_time);
Hope this helps.
Regards,
Sriram
  8 个评论
Pranav Krishnan
Pranav Krishnan 2020-3-24
Ah, a trivial mistake. Thanks for all your help! I'm just learning the syntax, and these mistakes have been valuable learnings :)
Sriram Tadavarty
Sriram Tadavarty 2020-3-24
Hi Pranav,
Ok sure. I suggest you to take this course, which will give you a head start for MATLAB.
While updating the comments too, first confirm and then provide the comments, if it is really throwing the error or not giving the output.
Thanks for the help.
Regards,
Sriram

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Engines & Motors 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by