How can I fix this problem ? 'Unrecognized function or variable 'X'' I get this message even when I type N and M which are constants.

1 view (last 30 days)
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
  2 Comments
Cris LaPierre
Cris LaPierre on 3 Nov 2020
I can run your function using made up numbers without getting an error message.
Please include the complete error message (all the red text), as well as the specific values you use that result in the error.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 3 Nov 2020
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
  2 Comments
Cris LaPierre
Cris LaPierre on 3 Nov 2020
To Image Analyst's point, did you mean to create a script instead of a function? None of the inputs are necessary, since they are either defined again inside the function, or are not used at all (f).
Shugufa Muheb
Shugufa Muheb on 3 Nov 2020
I actually wanted to create a script and now I removed the function from the top I got the same result you have shown here. Thank you so much for your help

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 3 Nov 2020
Your code is a script followed by some functions. But the functions never get called. All it does is to "clear all". You never then go on to define x0,t0,t,T,N,M,f,g or call del4() with those values. And since del4 never gets called, X is never assigned.
Also, you pass in M and N to del4() but then you immediately overwrite them with values of 4, so why bother passing them in?

KELVIN CALVIN JUNIOR
KELVIN CALVIN JUNIOR on 6 Apr 2023
my code is not running on my matlab and i really dont know what the problem is. can anyone help me out as this is giving me concern. the code is below, it is a homogeneous transformation matrix code for a robot.
%Link lengths
L1 = 75;
L2 = 130;
L3 = 125;
L4 = 60;
L5 = 130;
%D-H parameter of the robot
L(1) = Link ([0 L1 0 -pi/2]);
L(2) = Link ([0 0 L2 0]);
L(3) = Link ([0 0 L3 0]);
L(4) = Link ([0 0 0 pi/2]);
L(5) = Link ([0 L4+L5 0 0]);
%defining the rotation limits of the links
Q1_Min = 0;
Q1_Max = pi;
Q2_Min = (1/12)*pi;
Q2_Max = (11/12)*pi;
Q3_Min = 0;
Q3_Max = pi;
Q4_Min = 0;
Q4_Max = pi;
Q5_Min = 0;
Q5_Max = pi;
L(1).qlim=[Q1_Min Q1_Max];
L(2).qlim=[Q2_Min Q2_Max];
L(3).qlim=[Q3_Min Q3_Max];
L(4).qlim=[Q4_Min Q4_Max];
L(5).qlim=[Q5_Min Q5_Max];
Kelvin_Assignment = SerialLink(L, 'name', 'Kelvin_Assignment');
title ('workspace of Kelvin Assignment');
%homogenous
syms th1 th2 th3 th4 th5;
syms alhpa1 alpha2 alpha3 alpha4 alpha5;
syms d1 d2 d3 d4 d5;
syms a1 a2 a3 a4 a5;
T01 = [cos(th1) -cos(th1)*sin(th1) sin(alpha1)*sin(th1) a1*cos(th1);
sin(th1) cos(alpha1)*cos(th1) -sin(alpha1)*cos(th1) a1*sin(th1);
0 sin(alpha1) cos(alpha1) d1;
0 0 0 1];
T12 = [cos(th2) -cos(alpha2)*sin(th2) sin(alpha2)*sin(th2) a2*cos(th2);
sin(th2) cos(alpha2)*cos(th2) -sin(alpha2)*cos(th2) a2*sin(th2);
0 sin(alpha2) cos(alpha2) d2;
0 0 0 1];
T23 = [cos(th3) -cos(alpha3)*sin(th3) sin(alpha3)*sin(th3) a3*cos(th3);
sin(th3) cos(alpha3)*cos(th3) -sin(alpha3)*cos(th3) a3*sin(th3);
0 sin(alpha3) cos(alpha3) d3;
0 0 0 1];
T34 = [cos(th4) -cos(alpha4)*sin(th4) sin(alpha4)*sin(th4) a4*cos(th4);
sin(th4) cos(alpha4)*cos(th4) -sin(alpha4)*cos(th4) a4*sin(th4);
0 sin(alpha3) cos(alpha3) d4;
0 0 0 1];
T45 = [cos(th5) -cos(alpha5)*sin(th5) sin(alpha5)*sin(th5) a5*cos(th5);
sin(th5) cos(alpha5)*cos(th5) -sin(alpha5)*cos(th5) a5*sin(th5);
0 sin(alpha5) cos(alpha5) d5;
0 0 0 1];
T05 = T01*T12*T23*T34*T45;
%for the second coordinate
Q1 = 0;
Q2 = 0;
Q3 = 0;
Q4 = pi/2;
Q5 = 0;
%substituting the values of q1,q2,q3 and remembering thsat th1=Q1, th2=Q2
T05_Equations = double(subs(T05, [th1 th2 th3 th4 th5 alpha1 alpha2 alpha3 alpha4 alpha5 a1 a2 a3 a4 a5 d2 d3 d4 d5], ...
[0 0 0 0 0 -pi/2 0 0 0 pi/2 0 130 125 0 0 75 0 0 0 190]));
x_Equations = T05_Equations(1,4);
y_Equations = T05_Equations(2,4);
z_Equations = T05_Equations(3,4);
T05_Equations_SE3 = SE3(T05_Equations);
T05_Fkine_SE3 = Kelvin_Assignment.fkine([Q1 Q2 Q3 Q4 Q5]);
T05_fkine = double(Kelvin_Assignment.fkine([Q1 Q2 Q3 Q4 Q5]));
X_fkine = T05_fkine(1,4);
y_fkine = T05_fkine(2,4);
z_fkine = T05_fkine(3,4);

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!