Script as Objective Function in lsqcurvefit (Optimization tool)

2 views (last 30 days)
Hi, I have a script with a variable so that it simulates in Dymola and returns a vector. The objective is to fit this vector (TotalHeat) with measured values (ydata). When I give my script as the Objective Function in lsqcurvefit, MATLAB returns an error where the script have to be executed as a function.
When I give the Function Optimization tool returns:
-----------------------------
Optimization running.
Error running optimization.
Too many input arguments.
Please, could anybody help me? Here is the function to minimize:
function F= J1615_onezone_optimization_Dymola( x )
load ('D:\mfu-lja\workspaces\data.mat'); % load J1515_measurement.csv data
ydata=data(10755:11373,5)-16139999.35; % Heat Counter Wh
%Target folder
onezone = 'D:/mfu-lja/workspaces/onezone';
%dymolaM-command can execute every command that can be used in Dymola (find
%dymolaM in the MFiles-folder with your Dymola-installation)
dymolaM(['cd("',onezone,'/optimierung")']);
res=dymolaM('translateModel("Optimization.J1615_onezone_fixedHF")');
%loaddsin creates dsin.mat-file that can be manipulated easily
loaddsin([onezone,'/optimierung/dsin.txt']);
%load dsin to a struct
S=load('dsin.mat');
fixedHeatFlow=x(1); % This is the Variable name I need to set for the optimization.
dymolaM(['House.fixedHeatFlow.Q_flow=',num2str(fixedHeatFlow)]); %The value fixedHeatFlow is assigned to Q.flow
dymolaM('simulateModel("Optimization.J1615_onezone_fixedHF", stopTime=604800, numberOfIntervals=0, outputInterval=3600, resultFile="J1615_onezone_fixedHF")'); %Simulation in Dymola with the value Q.flow=x(1)
d=dymload(['D:\mfu-lja\workspaces\onezone\optimierung\J1615_onezone_fixedHF.mat']); %loads the results matrix of the simulation from '<file>.mat'.
TotalHeat=d.data_2(:,41); %TotalHeat is a vector with the consumption in Wh.
F=ydata-TotalHeat;
end

Accepted Answer

Alan Weiss
Alan Weiss on 14 Jan 2013
I still do not see what is going on here. What is the error you obtain?
I just noticed that the x0 parameter is one-dimensional. It seems that you are minimizing a one-dimensional function, and this function is the result of a simulation. Perhaps you would do better to write the sum of squares as your objective function and use fminbnd as your minimizer. fminbnd does not attempt to estimate derivatives, so might be more suitable for your problem in any case. See this section for a discussion of problems in optimizing simulations .
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (2)

Shashank Prasanna
Shashank Prasanna on 14 Jan 2013
It appears that "J1615_onezone_optimization_Dymola_function" should itself represent the curve you are trying to fit from your function call: lsqcurvefit(@J1615_onezone_optimization_Dymola_function,x0,xdata,ydata,[],[],options)
It is a bad idea to load data into the function workspace and using paths, this can significantly slow down your optimization and cause issues for convergence as well. To comment on what would be the best solver for you problem we will need more information on what "Dymola" is. Is it smooth? is it continuous everywhere? If you are unsure and have Global Optim I would recommend starting with patternsearch.

Alan Weiss
Alan Weiss on 14 Jan 2013
I don't know what is going on. Can you please give your function call, something like
[x,res] = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
We will have a better chance at diagnosing your problem if we see exactly how you called lsqcurvefit, and what the size of each argument is.
And for programming efficiency, you might want to load the data just once, outside the function J1615_onezone_optimization_Dymola, and pass the data in via an anonymous function or nested function, as explained here.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Luis
Luis on 14 Jan 2013
Thank you for your reply. In the next script, the data are loaded outside the function. My case is not a typical function (f.e. x(1)+x(2)xdata). The parameter I am looking for optimize is solved in Dymola, not with a function. Do you know, if I can do that with lsqcurvefit or is there another better method?
%Target folder
onezone = 'D:/mfu-lja/workspaces/onezone';
load ('D:\mfu-lja\workspaces\data.mat'); % load J1515_measurement.csv data
xdata=data(10755:10843,1)-63331200; % Simulate (1day =10755:10843)
ydata=data(10755:10843,5)-16139999.35; % Heat Counter Wh
x0=5000;
% options for the algorithm
options = optimset('Diagnostics','off','Display','off','FunValCheck','off','MaxIter',1e+6,'MaxFunEvals',1e+6,'TolFun',1e-6,'TolX',1e-6);
format short e
[x,resnorm] = lsqcurvefit(@J1615_onezone_optimization_Dymola_function,x0,xdata,ydata,[],[],options)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!