Optimization of system ODE

2 visualizzazioni (ultimi 30 giorni)
Nicholas Moung
Nicholas Moung il 21 Gen 2021
Commentato: Nicholas Moung il 1 Feb 2021
Hello Matlabers!
I'm using fsolve function for optimization of system of ODE. And I'm facing difficulty now. Could someone advise me with this please? I'm just beginner in using Matlab and I do appreciate your advise.
The functions file is expressed below.
------------------------------------------------------
function fval= SimFnc(X)
%Initial values
dt=1/32;
g=9.8065;
t=1:2003; %number of points
%Importing data from STAT
STAT=importdata('STAT.txt');
nx= STAT(:,10);
ny= STAT(:,11);
nz= STAT(:,12);
Wx_d= STAT(:,7);
Wx_r=deg2rad(Wx_d);
% Wx = interp1(Wx_r,t);
Wy_d= STAT(:,8);
Wy_r=deg2rad(Wy_d);
% Wy = interp1(Wy_r,t);
Wz_d= STAT(:,9);
Wz_r=deg2rad(Wz_d);
Theda_iz=STAT(:,13);
Gamma_iz=STAT(:,14);
Theda_izr=deg2rad(Theda_iz);
Gamma_izr=deg2rad(Gamma_iz);
Alpha_iz=STAT(:,29);
Alpha_izr=deg2rad(Alpha_iz);
Beta_iz=STAT(:,30);
Beta_izr=deg2rad(Beta_iz);
V_iz =STAT(:,28);
%Initial values of Variables
Alpha(1)=0.1587/57.2958;
Beta(1)=0.0143/57.2958;
V(1)=58.0154;
Theda(1)=0.0025/57.2958;
Gamma(1)=0.0013/57.2958;
Erx=X(1);
Ery=X(2);
Erz=X(3);
Erv=X(4);
Erw=X(5);
%Calculating by Euler's method
for i=1:2002
ax=g*(nx(i)-sin(Theda(i)));
ay=g*(ny(i)-cos(Theda(i))*cos(Gamma(i)));
az=g*(nz(i)+cos(Theda(i))*sin(Gamma(i)));
csB=cos(Beta(i));
csBinv=1/csB;
ssB=sin(Beta(i));
csA=cos(Alpha(i));
ssA=sin(Alpha(i));
axv=ax/V(i);
ayv=ay/V(i);
azv=az/V(i);
Theda(i+1)=Theda(i)+dt*(((Wy_r(i)*Ery*sin(Gamma(i)))+(Wz_r(i)*Erz*cos(Gamma(i)))));
Gamma(i+1)=Gamma(i)+dt*((Wx_r(i)*Erx-(tan (Theda(i))*(Wy_r(i)*Ery*cos(Gamma(i))-Wz_r(i)*Erz*sin(Gamma(i))))));
Alpha(i+1)= Alpha(i)+ dt*( Wz_r(i)*Erz-csBinv*Erv(ssA*(axv-Wy_r(i)*Ery*ssB)+csA*(ayv+Wx_r(i)*Erx*ssB)) );
Beta(i+1)=Beta(i)+dt*( azv*csB-csA*(axv*Erv*ssB-Wy_r(i))+ssA*(ayv*ssB+Wx_r(i)*Erx) );
V(i+1)=V(i)+dt*(ax*csA*csB-ay*Erw*ssA*csB+az*ssB );
fval(1,1,:)=Theda;
fval(2,1,:)=Gamma;
fval(3,1,:)=Alpha;
fval(4,1,:)=Beta;
fval(5,1,:)=V;
end
end
The optimization file is expressed below.
-------------------------------------------------------
%Solving NLAE
clc
clear
zg=input('Enter initial guess');
format short g
%options = optimoptions('fsolve','Display','iter');
%options=optimoptions('fsolve','Algorithm','trust-region');
%options=optimoptions('fsolve','Algorithm','trust-region-dogleg');
options=optimoptions('fsolve','Algorithm','levenberg-marquardt');
%Sol=fsolve(@SimFnc,zg,options)
% @fncSi = @(t,y)myfun(t,y,Wx,Wy,Wz);
[X,fval,exitflag,output,hessian] =fsolve(@SimFnc,zg,options)
  2 Commenti
Sargondjani
Sargondjani il 21 Gen 2021
Modificato: Sargondjani il 21 Gen 2021
It is not clear what your problem is. Also it is helpful if you provide a denser version of your question, 90% of your code just seems to define some parameters. (ie. provide a minimum working example).
Nicholas Moung
Nicholas Moung il 21 Gen 2021
You are right. I have done the integration in decrete time and I need to identify some parameters (Erx=X(1); Ery=X(2); Erz=X(3); Erv=X(4); Erw=X(5) of the system by using fsolve function. I'm looking forward to hearing your further advise. Thank you!

Accedi per commentare.

Risposte (2)

Sargondjani
Sargondjani il 22 Gen 2021
It is still not clear what your problem is. Do you get an error? Do you get a different solution than you expected?
Anyway fsolve will try to find the zeros of the output, but your output are large vectors, so they can not all be zero by setting 5 parameters. In that case it is probably better to use the function 'lsqnonlin'.
Other notes:
  • you should preallocate variables: Theda = NaN(1,2002+1); before the loop;
  • if you use lsqnonlin, i would recommend to use output arguments in a matrix (not 3d array): fval(1,:) = Theda;
  1 Commento
Nicholas Moung
Nicholas Moung il 1 Feb 2021
Hello Sir!
I did everything you suggested and I'm still getting some errors as expressed below.
Array indices must be positive integers or logical values.
Error in SimFnc5 (line 74)
Alpha(i+1)= Alpha(i)+ dt*( Wz_r(i)*Erz-csBinv*Erv(ssA*(axv-Wy_r(i)*Ery*ssB)+csA*(ayv+Wx_r(i)*Erx*ssB)) );
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in Sol_SimFnc (line 12)
[X,fval,exitflag,output,hessian]=fsolve(@SimFnc5,zg,options)%jacobian,
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Could you please tell me what to do further? Thank you so much!

Accedi per commentare.


Star Strider
Star Strider il 22 Gen 2021

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by