MATLAB: How to fix this problem ? ‘Unrecognized function or variable ‘X” I get this message even when I type N and M which are constants.

for loopMATLABmatlab functionode

clear all
function [X,alpha, lambda]= del4(x0,t0,t,T,N,M,f,g)
N=4;
M=4;
dt=1/N;
x0=0;
t0=0; %initial time
T=1; %final time
t = t0:dt:T;
%en vektor till Kapitalet (X)
X=zeros(1,N+1); % allocate the result of X
% X(1)=x0; % the initial value of X
j=numel(X); %the number of X values
%vector for lagrange multiplier (lambda)
lambda = zeros(1,N); % preallocation of lambda
g = @(X) 2*sqrt(X); % definiera fuktionen g
dg = @(X) 1./sqrt(X); %derivatan av g
d_g= dg(X);
%vector for capital updating (alpha)
alpha = zeros(1, N+1); %preallocation of alpha
%vector for start guess for capital at each time
A= [2 4 5 6 7]; % start guess
g = @(X) 2*sqrt(X); % ddefine function g
d_g = zeros(1,N+1); % define g'(X)
for k = 1:j
dg = @(X) 1./sqrt(X);
d_g(k) = dg(k);
end
% functions
func_1 = f1(X); % function No.1

func_2 = f2(X); % function No.2

for i = 1:M
for n = 1:N-1
lambda(N) = d_g(N); %initial lambda value
func_1 = f1(X);
func_2 = f2(X);
lambda(n) = lambda(n+1) + dt * func_2(1,n) * lambda(n+1);
for n=1:N-1
for index = 1:length(t)
x0 = x0 + A(index);
Y(index) = x0;
X(n)=Y(index);
func_1 = f1(X);
func_2 = f2(X);
X(n+1) = X(n) + dt * (func_2(2,n)- 1/(lambda(n+1).^(3/5)));
end
end
end
alpha = 1./((lambda).^(3/5));
C = norm(X(i) - alpha(i));
end
plot(t, X)
hold on
plot(alpha)
end
%functions and the derivatives
% function No.1
function func_1 = f1(X)
func_1 = zeros(2,length(X));
for l = 1:length(X)
f_1 = X(l);
df_1 = 1;
func_1(:,l) = [f_1;df_1];
end
end
% function No.2
function func_2 = f2(X)
func_2 = zeros(2,length(X));
for p = 1:length(X)
f_2 = X(p) + (X(p).^2)/10;
df_2 = 1 + X(p)./5;
func_2(:,p)= [f_2; df_2];
end
end

Best Answer

Did you run your function? You have to call it.
% Define function inputs
x0 = 1;
t0 = 0;
t = 1;
T = 10;
N = 10;
M = 100;
f = 25;
g = 9.81;
% Call function
[X,alpha, lambda]= del4(x0,t0,t,T,N,M,f,g);
% Now I can call X, N, and M because they have been created and exist in the Workspace
X
X = 1×5
816.0000 840.0000 864.0000 907.0711 0
N
N = 10
M
M = 100
%% Define in-file functions at the bottom of your script
function [X,alpha, lambda]= del4(x0,t0,t,T,N,M,f,g)
N=4;
M=4;
dt=1/N;
x0=0;
t0=0; %initial time
T=1; %final time
t = t0:dt:T;
%en vektor till Kapitalet (X)
X=zeros(1,N+1); % allocate the result of X
% X(1)=x0; % the initial value of X
j=numel(X); %the number of X values
%vector for lagrange multiplier (lambda)
lambda = zeros(1,N); % preallocation of lambda
g = @(X) 2*sqrt(X); % definiera fuktionen g
dg = @(X) 1./sqrt(X); %derivatan av g
d_g= dg(X);
%vector for capital updating (alpha)
alpha = zeros(1, N+1); %preallocation of alpha
%vector for start guess for capital at each time
A= [2 4 5 6 7]; % start guess
g = @(X) 2*sqrt(X); % ddefine function g
d_g = zeros(1,N+1); % define g'(X)
for k = 1:j
dg = @(X) 1./sqrt(X);
d_g(k) = dg(k);
end
% functions
func_1 = f1(X); % function No.1

func_2 = f2(X); % function No.2

for i = 1:M
for n = 1:N-1
lambda(N) = d_g(N); %initial lambda value
func_1 = f1(X);
func_2 = f2(X);
lambda(n) = lambda(n+1) + dt * func_2(1,n) * lambda(n+1);
for n=1:N-1
for index = 1:length(t)
x0 = x0 + A(index);
Y(index) = x0;
X(n)=Y(index);
func_1 = f1(X);
func_2 = f2(X);
X(n+1) = X(n) + dt * (func_2(2,n)- 1/(lambda(n+1).^(3/5)));
end
end
end
alpha = 1./((lambda).^(3/5));
C = norm(X(i) - alpha(i));
end
plot(t, X)
hold on
plot(alpha)
hold off
end
%functions and the derivatives
% function No.1
function func_1 = f1(X)
func_1 = zeros(2,length(X));
for l = 1:length(X)
f_1 = X(l);
df_1 = 1;
func_1(:,l) = [f_1;df_1];
end
end
% function No.2
function func_2 = f2(X)
func_2 = zeros(2,length(X));
for p = 1:length(X)
f_2 = X(p) + (X(p).^2)/10;
df_2 = 1 + X(p)./5;
func_2(:,p)= [f_2; df_2];
end
end