You are on page 1of 2

MATLAB code for inverse analysis

This program is designed for six-parameter optimization, and includes two MATLAB files:
main.m and objfunction.m. It will execute external programs pre.exe, ABAQUS, and
post.exe, which should be supplied by user to solve direct finite element analysis. The
interface files: initial.txt is for initial guess of parameters; param.txt stores the latest
parameters; history.txt records convergence history; test.txt and result.txt are experimental
and predicted data, respectively.
The following is the code for main.m.
clear; close all;
% Declaration of global variables
global Iter FunEvals ParamHistory c1 c2 c3 c4 c5 c6
global nob nop k0 M mu lamda Ef1 Ef0
load initial.txt
% Initial parameters
k0=initial(1,1); M=initial(1,2); mu=initial(1,3); lamda=initial(1,4);
Ef1=initial(1,5); Ef0=initial(1,6);
% The total number of optimizing parameters
nop=6;
% The scale coefficients for corresponding parameters
c1=1.0e11; c2=1.0e-5; c3=1.0e-9; c4=1.0e-9; c5=1.0e-14; c6=1.0e-11;
% Scale initial parameters
IniGuess=[k0*c1, M*c2, mu*c3, lamda*c4, Ef1*c5, Ef0*c6];
% Initialize counters
Iter=0; FunEvals=0; ParamHistory=[];
% Set optimization options.
options=optimset('TolFun',1e-7, 'TolX',1e-7, 'MaxIter',100)
%Solve the nonlinear least squares problem. It needs "objfunction.m"
[x, ErrNorm]=lsqnonlin(@objfunction,IniGuess,[0 0 0 0 0 0], [inf inf inf inf inf inf],options)
% Scale the optimized parameters to normal values
x1=x(1)/c1; x2=x(2)/c2; x3=x(3)/c3; x4=x(4)/c4; x5=x(5)/c5; x6=x(6)/c6;
OptParam=[x1 x2 x3 x4 x5 x6];
% Save the optimized parameters in "param.txt".
save param.txt OptParam -ascii
% Save the parameter evolution history in "history.txt".
ParamHistory=[ParamHistory; [Iter FunEvals x1 x2 x3 x4 x5 x6 ErrNorm]];
save history.txt ParamHistory -ascii
% Call external programs to solve direct problem and get "result.txt".
! del tempfile.*
! pre
! copy inputfile.inp tempfile.inp
! abaqus job=tempfile interactive
! post

The following is the code for objfunction.m.


function err=f(params)
global Iter FunEvals ParamHistory c1 c2 c3 c4 c5 c6
global nob nop k0 M mu lamda Ef1 Ef0
% Scale the parameters to normal values
x1=params(1)/c1; x2=params(2)/c2; x3=params(3)/c3;
x4=params(4)/c4; x5=params(5)/c5; x6=params(6)/c6;
OptParam=[x1 x2 x3 x4 x5 x6];
% Save the parameters in "param.txt".
save param.txt OptParam -ascii
% Read experimental data from "test.txt"
% The format is time in column 1 and response in column 2
load test.txt
TimeExp=test(:,1); ForceExp=test(:,2);
% The total number of observations
nob=length(TimeExp);
%Plot experimental response
plot(TimeExp,ForceExp,'g'), hold on
% Call external programs to solve direct problem.
% "pre.exe" reads "param.txt" and outputs "inputfile.inp"
% "post.exe" reads "tempfile.dat" and outputs "result.txt"
! del tempfile.*
! pre
! copy inputfile.inp tempfile.inp
! abaqus job=tempfile interactive
! post
% "result.txt" should have the same format of "test.txt"
% Read predicted data from "result.txt"
load result.txt
TimeCom=result(:,1); ForceCom=result(:,2);
%Plot predicted response
plot(TimeCom,ForceCom,'g'), hold on
% Error vector is difference between experimental and predicted forces
err=ForceExp-ForceCom;
% Calculate the norm of error vector
ErrNorm=sum(err.^2);
% Save the parameter evolution history in "history.txt".
ParamHistory=[ParamHistory; [Iter FunEvals x1 x2 x3 x4 x5 x6 ErrNorm]];
save history.txt ParamHistory -ascii
% Update the counters
FunEvals=FunEvals+1;
if rem(FunEvals,nop)==0
Iter=Iter+1;
end

You might also like