1

I have the following code to solve some ordinary differential equation(ODE). In order to relieve myself of the burden of typing a long expression in each iteration of the for loop I decided to define an anonymous function. When I defined the anonymous function before initializing x vector it turned out an error since it used the elements of the vector x in calculations. The anonymous function is as follows:

fnc = @(i) (-100 * (x(i) - cos(h * t(i))) - sin(h * t(i)));

In order to resolve the problem I initialized all elements of x to be zeros beforehand to avoid it complaining as follows:

Undefined function 'x' for input
arguments of type 'double'.

However, it turned out to mess up with solution depending on where I put the anonymous function. What may be the reason for this behaviour? My code can be found below, you can see the commented out regions to see where I tried to implement the anonymous function.

close all;
clear all;
clc;
t_s = 0; % Starting time 
t_f = 1; % Finishing time
h = 0.2; % Time step 
t = t_s:h:t_f;
N = (t_f - t_s) / h; % Number of time steps (excluding initial points)
% x = zeros(1, N + 1);
fnc = @(i) (-100 * (x(i) - cos(h * t(i))) - sin(h * t(i)));
x(1) = 1; % Initial condition given in the problem
x(2) = x(1) + h * fnc(1);
for in = 3:N+1
%    fnc = @(i) (-100 * (x(i) - cos(h * t(i))) - sin(h * t(i)));
   x(in) =  x(in - 1) + (3 / 2) * h * fnc(in - 1) + (-1 / 2) * h * fnc(in - 2); 
end

1 Answer 1

1

The value of x is "hardwired" into the anonymous function when you create it. The function internally stores the value that x has at that time, and will always use that. Subsequent changes to x have no effect on the function. See this example from the documentation:

For example, create a function handle to an anonymous function that requires coefficients a, b, and c.

a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c;

Because a, b, and c are available at the time you create parabola, the function handle includes those values. The values persist within the function handle even if you clear the variables:

clear a b c
x = 1;
y = parabola(x)
y =
31.5000

You can check which values are stored by the function using functions. For example:

>> x = [1 2 3 4];
>> fnc = @(i) (-100 * (x(i) - cos(h * t(i))) - sin(h * t(i)));
>> f = functions(fnc)
f = 
     function: '@(i)(-100*(x(i)-cos(h*t(i)))-sin(h*t(i)))'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> f.workspace{1}
ans = 
    x: [1 2 3 4]

If you need the function to depend on x, put x as an additional input (and call the function with the desired x each time):

fnc = @(i,x) (-100 * (x(i) - cos(h * t(i))) - sin(h * t(i)));
Sign up to request clarification or add additional context in comments.

3 Comments

Well thanks that was very explanatory can I define this function before defining or initializing x by the way?
@Vesnog No, unless x is declared as an input: fnc = @(i,x)... All variables that are not inputs to the function need to be defined, and their values are fixed regarding the function
Okay I understood now thanks for your effort, I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.