Invalid use of Operator error

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);

 Accepted Answer

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

Hey. I made the changes, and did a little more debugging (there were a few random errors). Invalid Operator still shows up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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*(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);
Hi Pranav, I don't see any errors with the code. Can you first perform these in the command prompt and try:
clc
clear all
Hope this helps.
Regards,
Sriram
Pranav Krishnan
Pranav Krishnan on 24 Mar 2020
Edited: Pranav Krishnan on 24 Mar 2020
Sorry, still not working.
Even tried rewriting the script, replacing the constants with parameters and calling the script as a function from command line. Is this script running for you? Output should just be 4 graphs. Thanks
Can you post the error here?
>> 1d_cond
1d_cond %error message starts (entire text in red in command prompt)
Error: Invalid use of operator.
I hope this is the function name you placed for this here. The function name shouldn't start with numbers. Here, is the output which I got when I run the code directly.
Ah, a trivial mistake. Thanks for all your help! I'm just learning the syntax, and these mistakes have been valuable learnings :)
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

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!