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일

0 개 추천

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

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 2020년 3월 24일
편집: Pranav Krishnan 2020년 3월 24일
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
Sriram Tadavarty
Sriram Tadavarty 2020년 3월 24일
Can you post the error here?
Pranav Krishnan
Pranav Krishnan 2020년 3월 24일
편집: Pranav Krishnan 2020년 3월 24일
>> 1d_cond
1d_cond %error message starts (entire text in red in command prompt)
Error: Invalid use of operator.
Sriram Tadavarty
Sriram Tadavarty 2020년 3월 24일
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.
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개)

카테고리

도움말 센터File Exchange에서 Simulink에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by