Thread Subject: function in m file

Subject: function in m file

From: ee

Date: 25 Nov, 2009 05:48:01

Message: 1 of 5

I need help to run this code in a M file, Is there any body who has an idea why this code is not runing
 %
% Main program for the numerical integration of
% the PDE modeling heat conduction in a solid slab.
%
 
global n length;
%
% set the initial conditions
%
n = 51; % Number of grid points (including boundaries)
length = 1; % Length of the slab
dx =length/(n-1);
%
% Initial conditions definition
%
for i = 1:n,
x (i) = (i-1)*dx;
tic (i) = 100*sin(pi*x(i)/length);
end
 tspan = linspace(0,1);
%
% Integration of the discretized PDE system

%
[time,temp] = ode15s('fcn',tspan,tic);
[lastp,nn] = size(time);
plot(temp(1,1:n)),hold,xlabel('x'),ylabel('T')
for i = 2:lastp,
plot(temp(i,1:n))
end
 
function fx = fcn (time,t)
global n length
%
% left boundary condition
%
t (1) = 0;
%
% right boundary condition
%
t (n) = 0;
%
% first-order derivative tx
%
tx = dss002 (0,length,n,t);
%
% second order derivative txx
%
txx = dss002 (0,length,n,tx);
%
% return the discretized value of the second order derivative
%
fx = txx';
end
function ux = dss002 (xl,xu,n;u)
%
% Function dss002 computes the first derivative, "ux", of a
% variable "u" over the spatial domain xl .le. x .le. xu from
% the classical three point, second order finite difference approximations.
%
% Inputs:
% -------
% xl = lower boundary value of x
% xu = upper boundary value of x
% n = number of grid points in the x domain
% including the boundary points
% u = one dimensional array containing the value of u at
% the n grid points for which the derivative is to be computed
%
% Outputs:
% -------
% ux = one dimensional array containing the numerical values of
% the derivatives of u at the n grid points
%
% Matlab version written by: Antonio Flores T./ April 2002
%
%
% Spatial increment
%
dx = (xu-xl)/(n-1);
r2fdx = 1/(2*dx);
nm1 = n-1;
%
% Equation (12)
%
ux (1) = r2fdx*(-3*u(1)+4*u(2)-u(3));
%
% Equation (9)
%
for i = 2:nm1,
ux (i) = r2fdx*(-u(i-1)+u(i+1));
end
%
% Equation (13)
%
ux (n) = r2fdx*(u(n-2)-4*u(n-1)+3*u(n));
%-- End of the dss002.m file --
end

Subject: function in m file

From: Jan Simon

Date: 25 Nov, 2009 07:17:19

Message: 2 of 5

Dear ee!

> I need help to run this code in a M file, Is there any body who has an idea why this code is not runing
> %
> % Main program for the numerical integration of
> % the PDE modeling heat conduction in a solid slab.
> % ...

Please tell us what "code is not running" means. Describe the output and the expectations, the exact error message (if one appears) and the corresponding line.

Kind regards, Jan

Subject: function in m file

From: ee

Date: 25 Nov, 2009 07:29:03

Message: 3 of 5

 Thank you very much,
this is the error msg "??? Error: File: mat.m Line: 32 Column: 1
Function definitions are not permitted at the prompt or in scripts."

with this code i would like to discretize a pde using the method of lines

The outpout should be the plot of my new set of equation

Subject: function in m file

From: Jan Simon

Date: 25 Nov, 2009 09:07:01

Message: 4 of 5

Dear ee!

> this is the error msg "??? Error: File: mat.m Line: 32 Column: 1
> Function definitions are not permitted at the prompt or in scripts."

You cannot define a function inside a script.
So at first read the documentation about functions and scripts.
Then I'd recomment to create functions also, because this reduces conflicts in the main workspace - but this may be a question of taste.

Kind regards, Jan

Subject: function in m file

From: Steven Lord

Date: 27 Nov, 2009 00:07:34

Message: 5 of 5


"ee " <sarkinkarfi@hotmail.com> wrote in message
news:heimbv$52t$1@fred.mathworks.com...
> Thank you very much,
> this is the error msg "??? Error: File: mat.m Line: 32 Column: 1
> Function definitions are not permitted at the prompt or in scripts."

Your M-file is a script file, not a function file.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html

As the error states, you cannot define a function inside of a script M-file
(or at the MATLAB prompt.)

Either convert your script file into a function file or move the functions
in the script file into their own function M-files and just call them from
in the script.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
programm ee 25 Nov, 2009 00:49:03
mfile ee 25 Nov, 2009 00:49:03
code ee 25 Nov, 2009 00:49:03
function ee 25 Nov, 2009 00:49:02
rssFeed for this Thread

Contact us at files@mathworks.com