# MATLAB Function (Solving an Error)

I have one file with the following code:

function fx=ff(x)
fx=x;

I have another file with the following code:

function g = LaplaceTransform(s,N)
g = ff(x)*exp(-s*x);

a=0;
b=1;

If=0;
h=(b-a)/N;
If=If+g(a)*h/2+g(b)*h/2;
for i=1:(N-1)
If=If+g(a+h*i)*h;
end;
If

Whenever I run the second file, I get the following error:

Undefined function or variable 'x'.

What I am trying to do is integrate the function g between 0 and 1 using trapezoidal approximations. However, I am unsure how to deal with x and that is clearly causing problems as can be seen with the error.

Any help would be great. Thanks.

-

Looks like what you're trying to do is create a function in the variable g. That is, you want the first line to mean,

"Let g(x) be a function that is calculated like this: ff(x)*exp(-s*x)",

rather than

"calculate the value of ff(x)*exp(-s*x) and put the result in g".

### Solution

You can create a subfunction for this

function result = g(x)
result = ff(x) * exp(-s * x);
end

Or you can create an anonymous function

g = @(x) ff(x) * exp(-s * x);

Then you can use g(a), g(b), etc to calculate what you want.

-
Ah, I guess that is probably what he meant. The function call/array access overloading makes it harder to infer intention... –  bnaul Oct 4 '11 at 7:12
Also, you want to return If not g. –  Nzbuu Oct 4 '11 at 9:28

You can also use the TRAPZ function to perform trapezoidal numerical integration. Here is an example:

%# parameters
a = 0; b = 1;
N = 100; s = 1;
f = @(x) x;

%# integration
X = linspace(a,b,N);
Y = f(X).*exp(-s*X);
If = trapz(X,Y)        %# value returned: 0.26423

%# plot
area(X,Y, 'FaceColor',[.5 .8 .9], 'EdgeColor','b', 'LineWidth',2)
grid on, set(gca, 'Layer','top', 'XLim',[a-0.5 b+0.5])
title('$\int_0^1 f(x) e^{-sx} \,dx$', 'Interpreter','latex', 'FontSize',14)

-

The error message here is about as self-explanatory as it gets. You aren't defining a variable called x, so when you reference it on the first line of your function, MATLAB doesn't know what to use. You need to either define it in the function before referencing it, pass it into the function, or define it somewhere further up the stack so that it will be accessible when you call LaplaceTransform.

Since you're trying to numerically integrate with respect to x, I'm guessing you want x to take on values evenly spaced on your domain [0,1]. You could accomplish this using e.g.

x = linspace(a,b,N);

EDIT: There are a couple of other problems here: first, when you define g, you need to use .* instead of * to multiply the elements in the arrays (by default MATLAB interprets multiplication as matrix multiplication). Second, your calls g(a) and g(b) are treating g as a function instead of as an array of function values. This is something that takes some getting used to in MATLAB; instead of g(a), you really want the first element of the vector g, which is given by g(1). Similarly, instead of g(b), you want the last element of g, which is given by g(length(g)) or g(end). If this doesn't make sense, I'd suggest looking at a basic MATLAB tutorial to get a handle on how vectors and functions are used.

-