Script as Objective Function in lsqcurvefit (Optimization tool)
5 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
0 Comments
More Answers (2)
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.
0 Comments
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!