Effacer les filtres
Effacer les filtres

Using lsqnonlin and getting the message Error line 218.

2 vues (au cours des 30 derniers jours)
imad dawood
imad dawood le 25 Sep 2023
Commenté : imad dawood le 13 Oct 2023
Plz help, I'm new in using lsqnonlin ,and I'm using it to find 3D coordinates of a moving dipole w/analytic equation of the form v= [1/r1-1/r2] where 'r' is a distance or the difference between 2 xyz coordinates in a 3D simulation setup one is the location of measure and the other is the moving position . I' have 2 r's in the equation so I need 2 xyz coordinates or 6 varibles. I wrote the following secript and a seprate function
clear all;
clc;
close all
L=[3,3,3]; %starting point of electrode's position
LEb=zeros(1,1);
for xax=linspace (-1.8/8,1.8/8,10) %% step between electrode x-axis
for zax=linspace( 0,6,10) %% step between electrode in z-axis
w=8.*cos(xax/2); % equation govening the y-axis movement
LE=[(L(1)+xax).*10, w*L(2),L(3)+zax]; %array with size and shape of the of the electrodes
% three Dimmensional Plot of the Electroede system
figure(1)
p1= plot3((L(1)+xax).*10,w*L(2),L(3)+zax,'ms','LineWidth',0.5,'DisplayName','Posative Monopole');
hold on
grid on
LEb=[LEb, LE]; %to add an element to the array in order to start from one and make it divisible by three
end
end
LEB=buffer(LEb(2:end),3); %% Matrix coordinates of all electrodes - row (x,y,z) and column is number of electrodes
a=length(LEB);
[r,c,m]=size(LEB);
%% MOVING DIPOLE
Ls1_P=[1.5, 1.4,1.6]; %% location of positive monopole
Ls2_N=[1.6, 1.5,1.7]; % % location of negative monopole
Ls_PD1=zeros(1);
Ls_ND1=zeros(1);
%% (DIPOLE PATH) movement in straight line
t=0:0.05:2.95; %straight line time x
w=10.*t +10 ; %% straight line formula along the x-axis
g= -t+1; %% straight line formula along the y-axis
Q=2.*t + 2.0 ; %% straight line formula along the z-axis
for j=1:length(t)
Lsm_PD1=[Ls1_P(1)+w(j),Ls1_P(2)+g(j),Ls1_P(3)+Q(j)]; %% creating array of the points coordinate of moving postive charge
Lsm_ND1=[Ls2_N(1)+w(j),Ls2_N(2)+g(j),Ls2_N(3)+Q(j)]; %% creating array of the points coordinate of moving negative charge
Ls_PD1=[Ls_PD1, Lsm_PD1]; %% storing the iterations of +ve charge
Ls_ND1=[Ls_ND1, Lsm_ND1]; %% storing the iterations of -ve charge
end
Ls_PD1_B=buffer(Ls_PD1(2:end),3,0,'nodelay'); %% creating matrix of moving positive charge by dividing by (3)with no overley and excluding the 1st one
Ls_ND1_B=buffer(Ls_ND1(2:end),3,0,'nodelay'); %% creating matrix of moving negative charge by dividing by (3)with no overley
[r_1,c_1,m_1]=size(Ls_PD1_B);
fn=length(Ls_PD1_B);
[r_2, c_2, m_2]=size(Ls_ND1_B);
%% THREE DIMMENSIONAL PLOT OF THE DIPOLE MOVEMENT
for i=1:c_1
grid on
p2=plot3(Ls_ND1_B(1,i),Ls_ND1_B(2,i),Ls_ND1_B(3,i),'bo','MarkerSize',8,'lineWidth',2, 'DisplayName','Posative Monopole'); % plot x,y,z of negative charge for i iterations
hold on
p3=plot3(Ls_PD1_B(1,i),Ls_PD1_B(2,i),Ls_PD1_B(3,i),'ro','MarkerSize',8,'lineWidth',2,'DisplayName','Negative Monopole') ;% plot x,y,z of negative charge for i iteration
end
pause(1)
hold on
xlabel('X axes')
ylabel('Y axes')
zlabel('Z axes')
K= 8.99*10^9; %Columbs constant
q=1.602*10^-6; % Charge of an electron with permitivity
for i=1:c_1 % No. of steps of movement
for n=1:c %No. of Electrodes
%%Dipole equation
V1D(n,i)= q*K *( (1./sqrt((LEB(1,n)-Ls_PD1_B(1,i)).^2+(LEB(2,n)-Ls_PD1_B(2,i)).^2+(LEB(3,n)-Ls_PD1_B(3,i)).^2 )) - (1./sqrt((LEB(1,n)-Ls_ND1_B(1,i)).^2+(LEB(2,n) - Ls_ND1_B(2,i)).^2+(LEB(3,n) - Ls_ND1_B(3,i)).^2 ))); % (analytic equation of the dipole) voltage of all electrodes due to all movment steps % voltage of all electrodes at all 61 steps of movement
V1DT(n,1)= q*K *( (1./sqrt((LEB(1,n) - Ls_PD1_B(1,1)).^2 + (LEB(2,n) - Ls_PD1_B(2,1)).^2+(LEB(3,n) - Ls_PD1_B(3,1)).^2 )) - (1./sqrt((LEB(1,n) - Ls_ND1_B(1,1)).^2+(LEB(2,n)-Ls_ND1_B(2,1)).^2+(LEB(3,n)-Ls_ND1_B(3,1)).^2 ))); % voltage of all electrodes due to a single step ()
v=V1D();
end
end
[r_3,c_3,m_3]=size(V1D(n,i));
%% PLOTING THE VALUES OF ALL ELECTRODE VOLTAGES
%% AT THEIR RELATIVE STEP MOVEMENT
figure(2)
plot(v);
title([' Number of Electrode Voltages = ',num2str(c) , ' Due to Dipole movement steps = ' num2str(c_1)])
xlabel('Number of Dipole mvement steps')
ylabel(' Voltage of Electrodes( Volts) ')
x0 = [11; 2;3;11;2;3]; %intial values for lsqnonlin
options = optimoptions(@lsqnonlin,'Algorithm','levenberg-marquardt','MaxFunctionEvaluations',4000);%'Display','iter'
[NLSE,resnorm,residual,exitflag,output]= lsqnonlin(@(x) allfunlsq(x,LEB, q, K,V1D,c,n, c_1) ,x0,[],[],options);
and the seprate function script is :-
function err = allfunlsq(x,LEB, q, K,V1D,c,n, c_1)
j=1;
for i=j:c_1
Ls_PD1_B(1,i) = x(1);
Ls_PD1_B(2,i) = x(2);
Ls_PD1_B(3,i) = x(3);
Ls_ND1_B(1,i) = x(4);
Ls_ND1_B(2,i) = x(5);
Ls_ND1_B(3,i) = x(6);
V1Dprdct(n,1)=0;
for n=1:c
V1Dprdct(n,j)= q*K *( (1./sqrt((LEB(1,n) - Ls_PD1_B(1,j)).^2 + (LEB(2,n) - Ls_PD1_B(2,j)).^2+(LEB(3,n) - Ls_PD1_B(3,j)).^2 )) - (1./sqrt((LEB(1,n) - Ls_ND1_B(1,j)).^2+(LEB(2,n)-Ls_ND1_B(2,j)).^2+(LEB(3,n)-Ls_ND1_B(3,j)).^2 )));
j=j+1;
V1Dprdct(n,j)=V1Dprdct(n,i);
err(n,i)=V1D(n,i) - V1Dprdct(n,i);
end
end
end
when I run the code I get the following message"
Index in position 2 exceeds array bounds. Index must not exceed 1.
Error in allfunlsq (line 17)
V1Dprdct(n,j)= q*K *( (1./sqrt((LEB(1,n) - Ls_PD1_B(1,j)).^2 + (LEB(2,n) - Ls_PD1_B(2,j)).^2+(LEB(3,n) - Ls_PD1_B(3,j)).^2 )) - (1./sqrt((LEB(1,n) - Ls_ND1_B(1,j)).^2+(LEB(2,n)-Ls_ND1_B(2,j)).^2+(LEB(3,n)-Ls_ND1_B(3,j)).^2 )));
Error in dipolelsq>@(x)allfunlsq(x,LEB,q,K,V1D,c,n,c_1) (line 115)
[NLSE,resnorm,residual,exitflag,output]= lsqnonlin(@(x) allfunlsq(x,LEB, q, K,V1D,c,n, c_1) ,x0,[],[],options);
Error in lsqnonlin (line 218)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in dipolelsq (line 115)
[NLSE,resnorm,residual,exitflag,output]= lsqnonlin(@(x) allfunlsq(x,LEB, q, K,V1D,c,n, c_1) ,x0,[],[],options);
Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue

Réponses (1)

Walter Roberson
Walter Roberson le 25 Sep 2023
Déplacé(e) : Walter Roberson le 25 Sep 2023
clear all;
clc;
close all
L=[3,3,3]; %starting point of electrode's position
LEb=zeros(1,1);
for xax=linspace (-1.8/8,1.8/8,10) %% step between electrode x-axis
for zax=linspace( 0,6,10) %% step between electrode in z-axis
w=8.*cos(xax/2); % equation govening the y-axis movement
LE=[(L(1)+xax).*10, w*L(2),L(3)+zax]; %array with size and shape of the of the electrodes
% three Dimmensional Plot of the Electroede system
figure(1)
p1= plot3((L(1)+xax).*10,w*L(2),L(3)+zax,'ms','LineWidth',0.5,'DisplayName','Posative Monopole');
hold on
grid on
LEb=[LEb, LE]; %to add an element to the array in order to start from one and make it divisible by three
end
end
LEB=buffer(LEb(2:end),3); %% Matrix coordinates of all electrodes - row (x,y,z) and column is number of electrodes
a=length(LEB);
[r,c,m]=size(LEB);
%% MOVING DIPOLE
Ls1_P=[1.5, 1.4,1.6]; %% location of positive monopole
Ls2_N=[1.6, 1.5,1.7]; % % location of negative monopole
Ls_PD1=zeros(1);
Ls_ND1=zeros(1);
%% (DIPOLE PATH) movement in straight line
t=0:0.05:2.95; %straight line time x
w=10.*t +10 ; %% straight line formula along the x-axis
g= -t+1; %% straight line formula along the y-axis
Q=2.*t + 2.0 ; %% straight line formula along the z-axis
for j=1:length(t)
Lsm_PD1=[Ls1_P(1)+w(j),Ls1_P(2)+g(j),Ls1_P(3)+Q(j)]; %% creating array of the points coordinate of moving postive charge
Lsm_ND1=[Ls2_N(1)+w(j),Ls2_N(2)+g(j),Ls2_N(3)+Q(j)]; %% creating array of the points coordinate of moving negative charge
Ls_PD1=[Ls_PD1, Lsm_PD1]; %% storing the iterations of +ve charge
Ls_ND1=[Ls_ND1, Lsm_ND1]; %% storing the iterations of -ve charge
end
Ls_PD1_B=buffer(Ls_PD1(2:end),3,0,'nodelay'); %% creating matrix of moving positive charge by dividing by (3)with no overley and excluding the 1st one
Ls_ND1_B=buffer(Ls_ND1(2:end),3,0,'nodelay'); %% creating matrix of moving negative charge by dividing by (3)with no overley
[r_1,c_1,m_1]=size(Ls_PD1_B);
fn=length(Ls_PD1_B);
[r_2, c_2, m_2]=size(Ls_ND1_B);
%% THREE DIMMENSIONAL PLOT OF THE DIPOLE MOVEMENT
for i=1:c_1
grid on
p2=plot3(Ls_ND1_B(1,i),Ls_ND1_B(2,i),Ls_ND1_B(3,i),'bo','MarkerSize',8,'lineWidth',2, 'DisplayName','Posative Monopole'); % plot x,y,z of negative charge for i iterations
hold on
p3=plot3(Ls_PD1_B(1,i),Ls_PD1_B(2,i),Ls_PD1_B(3,i),'ro','MarkerSize',8,'lineWidth',2,'DisplayName','Negative Monopole') ;% plot x,y,z of negative charge for i iteration
end
pause(1)
hold on
xlabel('X axes')
ylabel('Y axes')
zlabel('Z axes')
K= 8.99*10^9; %Columbs constant
q=1.602*10^-6; % Charge of an electron with permitivity
for i=1:c_1 % No. of steps of movement
for n=1:c %No. of Electrodes
%%Dipole equation
V1D(n,i)= q*K *( (1./sqrt((LEB(1,n)-Ls_PD1_B(1,i)).^2+(LEB(2,n)-Ls_PD1_B(2,i)).^2+(LEB(3,n)-Ls_PD1_B(3,i)).^2 )) - (1./sqrt((LEB(1,n)-Ls_ND1_B(1,i)).^2+(LEB(2,n) - Ls_ND1_B(2,i)).^2+(LEB(3,n) - Ls_ND1_B(3,i)).^2 ))); % (analytic equation of the dipole) voltage of all electrodes due to all movment steps % voltage of all electrodes at all 61 steps of movement
V1DT(n,1)= q*K *( (1./sqrt((LEB(1,n) - Ls_PD1_B(1,1)).^2 + (LEB(2,n) - Ls_PD1_B(2,1)).^2+(LEB(3,n) - Ls_PD1_B(3,1)).^2 )) - (1./sqrt((LEB(1,n) - Ls_ND1_B(1,1)).^2+(LEB(2,n)-Ls_ND1_B(2,1)).^2+(LEB(3,n)-Ls_ND1_B(3,1)).^2 ))); % voltage of all electrodes due to a single step ()
v=V1D();
end
end
[r_3,c_3,m_3]=size(V1D(n,i));
%% PLOTING THE VALUES OF ALL ELECTRODE VOLTAGES
%% AT THEIR RELATIVE STEP MOVEMENT
figure(2)
plot(v);
title([' Number of Electrode Voltages = ',num2str(c) , ' Due to Dipole movement steps = ' num2str(c_1)])
xlabel('Number of Dipole mvement steps')
ylabel(' Voltage of Electrodes( Volts) ')
x0 = [11; 2;3;11;2;3]; %intial values for lsqnonlin
options = optimoptions(@lsqnonlin,'Algorithm','levenberg-marquardt','MaxFunctionEvaluations',4000);%'Display','iter'
[NLSE,resnorm,residual,exitflag,output]= lsqnonlin(@(x) allfunlsq(x,LEB, q, K,V1D,c,n, c_1) ,x0,[],[],options);
Name Size Bytes Class Attributes K 1x1 8 double LEB 3x100 2400 double Ls_ND1_B 3x1 24 double Ls_PD1_B 3x1 24 double V1D 100x60 48000 double V1Dprdct 100x1 800 double c 1x1 8 double c_1 1x1 8 double i 1x1 8 double j 1x1 8 double n 1x1 8 double q 1x1 8 double x 6x1 48 double
ans = 1×2
1 1
Name Size Bytes Class Attributes K 1x1 8 double LEB 3x100 2400 double Ls_ND1_B 3x1 24 double Ls_PD1_B 3x1 24 double V1D 100x60 48000 double V1Dprdct 100x2 1600 double ans 1x2 16 double c 1x1 8 double c_1 1x1 8 double err 1x1 8 double i 1x1 8 double j 1x1 8 double n 1x1 8 double part1 1x1 8 double q 1x1 8 double x 6x1 48 double
ans = 1×2
2 2
Index in position 2 exceeds array bounds. Index must not exceed 1.

Error in solution>allfunlsq (line 98)
part1 = sqrt((LEB(1,n) - Ls_PD1_B(1,j)).^2 + (LEB(2,n) - Ls_PD1_B(2,j)).^2+(LEB(3,n) - Ls_PD1_B(3,j)).^2 );

Error in solution>@(x)allfunlsq(x,LEB,q,K,V1D,c,n,c_1) (line 83)
[NLSE,resnorm,residual,exitflag,output]= lsqnonlin(@(x) allfunlsq(x,LEB, q, K,V1D,c,n, c_1) ,x0,[],[],options);

Error in lsqnonlin (line 242)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});

Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue.
function err = allfunlsq(x,LEB, q, K,V1D,c,n, c_1)
j=1;
for i=j:c_1
Ls_PD1_B(1,i) = x(1);
Ls_PD1_B(2,i) = x(2);
Ls_PD1_B(3,i) = x(3);
Ls_ND1_B(1,i) = x(4);
Ls_ND1_B(2,i) = x(5);
Ls_ND1_B(3,i) = x(6);
V1Dprdct(n,1)=0;
for n=1:c
whos, [j, n]
part1 = sqrt((LEB(1,n) - Ls_PD1_B(1,j)).^2 + (LEB(2,n) - Ls_PD1_B(2,j)).^2+(LEB(3,n) - Ls_PD1_B(3,j)).^2 );
V1Dprdct(n,j)= q*K *( (1./part1) - (1./sqrt((LEB(1,n) - Ls_ND1_B(1,j)).^2+(LEB(2,n)-Ls_ND1_B(2,j)).^2+(LEB(3,n)-Ls_ND1_B(3,j)).^2 )));
j=j+1;
V1Dprdct(n,j)=V1Dprdct(n,i);
err(n,i)=V1D(n,i) - V1Dprdct(n,i);
end
end
end
First time through in your function, j starts at 1 and j starts at 1. You assign to LS_PD1_B(1:3,1) and LS_ND1_B(1:3,1) . Then in the for n loop you access LS_PD1_B(1,j) and the first time through that is fine because j is 1 and you have written up to column 1 in LS_PD1_B . But inside the for n loop, you increment j=j+1 so j becomes through, so when n becomes 2, you try to access LS_PD1_B(1,2) which does not exist yet.
You cannot allow j to exceed i in that code because you have only written up to column i .
  1 commentaire
imad dawood
imad dawood le 13 Oct 2023
thank u Sir for your prompt and valueable help.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by