Invalid use of Operator error
3 views (last 30 days)
Show older comments
Pranav Krishnan
on 24 Mar 2020
Commented: Sriram Tadavarty
on 24 Mar 2020
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);
0 Comments
Accepted Answer
Sriram Tadavarty
on 24 Mar 2020
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 Comments
Sriram Tadavarty
on 24 Mar 2020
Hi Pranav,
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
More Answers (0)
See Also
Categories
Find more on Engines & Motors in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!