MATLAB Answers

code is giving error ,unable to get output graphs...

3 views (last 30 days)
Mukti Tomar
Mukti Tomar on 16 Sep 2021
Commented: Walter Roberson on 25 Sep 2021
l1 = 20; % length first link
l2 = 15; % length second link
l3 = 10; % length third link
theta1 = 0:0.1:pi/4; % all theta1 values
theta2 = 0:0.1:pi/2; % all theta2 values
theta3 = 0:0.1:pi; % all theta3 values
t0 = 0 ; tf = 5; % Integration Duration
tspan = [t0 tf];
[THETA1,THETA2,THETA3] = meshgrid(theta1,theta2,theta3); % generate a grid of theta1, theta2 and theta3 values
PHI = THETA1 + THETA2 + THETA3; %compute phi values
X = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute x coordinates
Y = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute y coordinates
P = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2); % compute p coordinates
Q = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2); % compute q coordinates
data1 = [X(:) Y(:) THETA1(:)]; % create x-y-theta1 dataset
data2 = [X(:) Y(:) THETA2(:)]; % create x-y-theta2 dataset
data3 = [X(:) Y(:) THETA3(:)]; % create x-y-theta2 dataset
plot(X(:),Y(:),'b.');
axis equal;
xlabel('X','fontsize',12)
ylabel('Y','fontsize',12)
title('X-Y coordinates for theta1,theta2 and theta3','fontsize',12)
opt = anfisOptions;
opt.InitialFIS = 7;
opt.EpochNumber = 150;
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
disp('--> Training first ANFIS network.')
anfis1 = anfis(data1,opt);
disp('--> Training second ANFIS network.')
opt.InitialFIS = 6;
anfis2 = anfis(data2,opt);
disp('--> Training third ANFIS network.')
opt.InitialFIS = 8;
anfis3 = anfis(data3,opt);
x = 0:0.1:2; % x coordinates for validation
y = 8:0.1:10; % y coordinates for validation
[X,Y] = meshgrid(x,y);
THETA3D = PHI - theta1 - theta2; %theta3 is deduced
c2 = (P.^2 + Q.^2 - l1^2 - l2^2)/(2*l1*l2);
s2 = sqrt(1 - c2.^2);
THETA2D = atan2(s2,c2); % theta2 is deduced
c1 = ((l1 + l2*c2)* P + l2*s2*Q )/(P.^2 + Q.^2);
s1 = sqrt(1 -c1.^2);
THETA1D = atan2(s1,c1); % theta1 is deduced
XY = [X(:) Y(:)];
THETA1P = evalfis(anfis1,XY); % theta1 predicted by anfis1
THETA2P = evalfis(anfis2,XY); % theta2 predicted by anfis2
THETA3P = evalfis(anfis3,XY); % theta3 predicted by anfis3
theta1diff = THETA1D(:) - THETA1P; % theta1diff
theta2diff = THETA2D(:) - THETA2P; % theta2diff
theta3diff = THETA3D(:) - THETA3P; % theta3diff
subplot(2,1,1);
plot(time,theta1diff);
xlabel('time (s)'),ylabel('THETA1D - THETA1P','fontsize',12)
title('Deduced theta1 - Predicted theta1 vs Time','fontsize',12)
subplot(2,1,2);
plot(time,theta2diff);
xlabel('time (s)'),ylabel('THETA2D - THETA2P','fontsize',12)
title('Deduced theta2 - Predicted theta2 vs Time','fontsize',12)
subplot(2,1,3);
plot(time,theta3diff);
xlabel('time (s)'),ylabel('THETA3D - THETA3P','fontsize',12)
title('Deduced theta3 - Predicted theta3 vs Time','fontsize',12)
>> MODIFIED3DOF
--> Training first ANFIS network.
Reference to non-existent field 'input'.
Error in anfis (line 181)
for iInput = 1:length(in_fismat.input)
Error in MODIFIED3DOF (line 35)
anfis1 = anfis(data1,opt);
  8 Comments
Walter Roberson
Walter Roberson on 16 Sep 2021
The validation calculations at the bottom are broken... I am working on figuring otu what would be meaningful.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Sep 2021
This is closer.
I had to speculate about what the validation was intended to do, and about what proper coordinates were.
There are challenges due to the fact that the values THETA1D, THETA2D, THETA3D are being calculated in 3D dimensions, but the querying is being done in two dimensions. I am not convinced that part is correct.
The code will fail when it is time to plot, as the plot() involves time which is a variable that is not defined. There does not appear to be any time-like coordinate in the calculation.
l1 = 20; % length first link
l2 = 15; % length second link
l3 = 10; % length third link
theta1 = 0:0.1:pi/4; % all theta1 values
theta2 = 0:0.1:pi/2; % all theta2 values
theta3 = 0:0.1:pi; % all theta3 values
t0 = 0 ; tf = 5; % Integration Duration
tspan = [t0 tf];
[THETA1,THETA2,THETA3] = meshgrid(theta1,theta2,theta3); % generate a grid of theta1, theta2 and theta3 values
PHI = THETA1 + THETA2 + THETA3; %compute phi values
X = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute x coordinates
Y = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute y coordinates
P = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2); % compute p coordinates
Q = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2); % compute q coordinates
data1 = [X(:) Y(:) THETA1(:)]; % create x-y-theta1 dataset
data2 = [X(:) Y(:) THETA2(:)]; % create x-y-theta2 dataset
data3 = [X(:) Y(:) THETA3(:)]; % create x-y-theta3 dataset
plot(X(:),Y(:),'b.');
axis equal;
xlabel('X','fontsize',12)
ylabel('Y','fontsize',12)
title('X-Y coordinates for theta1,theta2 and theta3','fontsize',12)
opt = anfisOptions;
opt.InitialFIS = 7;
opt.EpochNumber = 150;
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
disp('--> Training first ANFIS network.')
anfis1 = anfis(data1,opt);
disp('--> Training second ANFIS network.')
opt.InitialFIS = 6;
anfis2 = anfis(data2,opt);
disp('--> Training third ANFIS network.')
opt.InitialFIS = 8;
anfis3 = anfis(data3,opt);
THETA3D = PHI - THETA1 - THETA2; %theta3 is deduced
c2 = (P.^2 + Q.^2 - l1^2 - l2^2)./(2*l1*l2);
c2 = min(c2,1); %can slightly exceed 1 due to round-off
s2 = sqrt(1 - c2.^2);
THETA2D = atan2(s2,c2); % theta2 is deduced
c1 = ((l1 + l2.*c2) .* P + l2.*s2.*Q )./(P.^2 + Q.^2);
s1 = sqrt(1 -c1.^2);
THETA1D = atan2(s1,c1); % theta1 is deduced
xval = 0:0.1:2; % x coordinates for validation
yval = 8:0.1:10; % y coordinates for validation
[Xval,Yval] = meshgrid(xval,yval);
XYval = [Xval(:) Yval(:)];
THETA1P = evalfis(anfis1,XYval); % theta1 predicted by anfis1
THETA2P = evalfis(anfis2,XYval); % theta2 predicted by anfis2
THETA3P = evalfis(anfis3,XYval); % theta3 predicted by anfis3
F1 = scatteredInterpolant(X(:), Y(:), THETA1(:));
F2 = scatteredInterpolant(X(:), Y(:), THETA2(:));
F3 = scatteredInterpolant(X(:), Y(:), THETA3(:));
THETA1Di = F1(XYval(:,1), XYval(:,2));
THETA2Di = F2(XYval(:,1), XYval(:,2));
THETA3Di = F3(XYval(:,1), XYval(:,2));
theta1diff = THETA1Di(:) - THETA1P; % theta1diff
theta2diff = THETA2Di(:) - THETA2P; % theta2diff
theta3diff = THETA3Di(:) - THETA3P; % theta3diff
subplot(2,1,1);
plot(time,theta1diff);
xlabel('time (s)'),ylabel('THETA1D - THETA1P','fontsize',12)
title('Deduced theta1 - Predicted theta1 vs Time','fontsize',12)
subplot(2,1,2);
plot(time,theta2diff);
xlabel('time (s)'),ylabel('THETA2D - THETA2P','fontsize',12)
title('Deduced theta2 - Predicted theta2 vs Time','fontsize',12)
subplot(2,1,3);
plot(time,theta3diff);
xlabel('time (s)'),ylabel('THETA3D - THETA3P','fontsize',12)
title('Deduced theta3 - Predicted theta3 vs Time','fontsize',12)
  11 Comments
Walter Roberson
Walter Roberson on 25 Sep 2021
l1 = 20; % length first link
l2 = 15; % length second link
l3 = 10; % length third link
theta1 = 0:0.1:pi/4; % all theta1 values
theta2 = 0:0.1:pi/2; % all theta2 values
theta3 = 0:0.1:pi; % all theta3 values
[THETA1,THETA2,THETA3] = meshgrid(theta1,theta2,theta3); % generate a grid of theta1, theta2 and theta3 values
PHI = THETA1 + THETA2 + THETA3; %compute phi values
X = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute x coordinates
Y = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2) + l3 * cos(THETA1 + THETA2 + THETA3); % compute y coordinates
P = l1 * cos(THETA1) + l2 * cos(THETA1 + THETA2); % compute p coordinates
Q = l1 * sin(THETA1) + l2 * sin(THETA1 + THETA2); % compute q coordinates
data1 = [X(:) Y(:) THETA1(:)]; % create x-y-theta1 dataset
data2 = [X(:) Y(:) THETA2(:)]; % create x-y-theta2 dataset
data3 = [X(:) Y(:) THETA3(:)]; % create x-y-theta3 dataset
plot(X(:),Y(:),'b.');
axis equal;
xlabel('X','fontsize',12)
ylabel('Y','fontsize',12)
title('X-Y coordinates for theta1,theta2 and theta3','fontsize',12)
fprintf('-->%s\n','Start training first ANFIS network')
anfis1 = anfis(data1,7,150,[0,0,0,0]);%train first ANFIS network
fprintf('-->%s\n','Start training second ANFIS network')
anfis2 = anfis(data2,6,150,[0,0,0,0]);%train second ANFIS network
fprintf('-->%s\n','Start training third ANFIS network')
anfis3 = anfis(data3,8,150,[0,0,0,0]);%train third ANFIS network
THETA3D = PHI -(THETA1 + THETA2); %theta3 is deduced
c2 = (P.^2 + Q.^2 - l1^2 - l2^2)./(2*l1*l2);
c2 = min(c2,1); %can slightly exceed 1 due to round-off
s2 = sqrt(1 - c2.^2);
THETA2D = atan2(s2,c2); % theta2 is deduced
c1 = ((l1 + l2.*c2).*P + l2.*s2.*Q )./(P.^2 + Q.^2);
s1 = sqrt(1 -c1.^2);
THETA1D = atan2(s1,c1); % theta1 is deduced
x = 0:0.1:2; % x coordinates for validation
y = 8:0.1:10; % y coordinates for validation
[X,Y] = meshgrid(x,y);
XY = [X(:) Y(:)];
THETA1P = evalfis(anfis1, XY); % theta1 predicted by anfis1
THETA2P = evalfis(anfis2, XY); % theta2 predicted by anfis2
THETA3P = evalfis(anfis3, XY); % theta3 predicted by anfis3
F1 = scatteredInterpolant(X(:), Y(:), THETA1P(:));
F2 = scatteredInterpolant(X(:), Y(:), THETA2P(:));
F3 = scatteredInterpolant(X(:), Y(:), THETA3P(:));
THETA1Di = F1(XY(:,1), XY(:,2));
THETA2Di = F2(XY(:,1), XY(:,2));
THETA3Di = F3(XY(:,1), XY(:,2));
theta1diff = THETA1Di(:) - THETA1P; % theta1diff
theta2diff = THETA2Di(:) - THETA2P; % theta2diff
theta3diff = THETA3Di(:) - THETA3P; % theta3diff
subplot(3,1,1);
plot(theta1diff);
ylabel('THETA1D - THETA1P','fontsize',12)
title('Deduced theta1 - Predicted theta1','fontsize',12)
subplot(3,1,2);
plot(theta2diff);
ylabel('THETA2D - THETA2P','fontsize',12)
title('Deduced theta2 - Predicted theta2','fontsize',12)
subplot(3,1,3);
plot(theta3diff);
ylabel('THETA3D - THETA3P','fontsize',12)
title('Deduced theta3 - Predicted theta3','fontsize',12)

Sign in to comment.

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!