''Conversion to function_handle from double is not possible.''

I get this error when I run my code. I was looking up what this error means, but I can't figure it out in context of my code.. This is main code.
clc
clear all
global x1e x2e x3e x1r x2r x3r alfa12 alfa13 alfa23
%LLE podatci - ekstrakcija aromata smjesom TTEG i vode - 6 komponenata (heptan) na 60°C
%eksperimentalni podatci - ekstrakt
x1e = [0.0140 0.0175 0.0163 0.0188 0.0181 0.0227 0.0228 0.0214 0.0191 0.0208];
x2e = [0.0729 0.0696 0.106 0.1041 0.1107 0.0931 0.0732 0.0739 0.113 0.069];
x3e = 1 - x1e - x2e;
%eksperimentalni podatci - rafinat
x1r = [0.689 0.6289 0.485 0.4727 0.4332 0.6831 0.6967 0.6553 0.4507 0.6139];
x2r = [0.275 0.2855 0.4418 0.3501 0.4056 0.313 0.2716 0.271 0.4436 0.253];
x3r = 1 - x1r - x2r;
%Optimizacija NRTL parametara - Metoda Sorensena i Arlta
%1. stupanj optimizacije - opis fazne ravnoteze - geneticki algoritam
%parametri neslucajnosti
alfa12 = 0.3;
alfa13 = 0.3;
alfa23 = 0.2;
rng default
%poziv funkcije cilja
OF_A = @OF_2_6H_60C;
%definiranje strukture problema
problem.fitnessfcn = OF_A;
problem.nvars = 6;
problem.options = optimoptions('ga');
[x,fval] = ga(problem)
This is function I called in main code.
function [OF2] = OF_2_6H_60C(tau12,tau13,tau21,tau23,tau31,tau32)
global x1e x2e x3e x1r x2r x3r alfa12 alfa13 alfa23
syms tau12 tau13 tau21 tau23 tau31 tau32
G12 = exp(-alfa12*tau12);
G13 = exp(-alfa13*tau13);
G21 = exp(-alfa12*tau21);
G23 = exp(-alfa23*tau23);
G31 = exp(-alfa13*tau31);
G32 = exp(-alfa23*tau32);
A1e = (x2e.*tau21*G21 + x3e.*tau31*G31)./(x1e + x2e.*G21 + x3e.*G31) + (((x1e./(x1e + x2e.*G21 + x3e.*G31)).*(-(x2e.*tau21*G21 + x3e.*tau31*G31)./(x1e + x2e.*G21 + x3e.*G31))) + ((x2e.*G12)./(x1e.*G12 + x2e + x3e.*G32)).*(tau21 - (x1e.*tau12*G12 + x3e.*tau32*G32)./(x1e.*G12 + x2e + x3e.*G32))) + ((x3e.*G13./(x1e.*G13 + x2e.*G23 + x3e)).*(tau31 - (x1e.*tau13*G13 + x2e.*tau23*G23)./(x1e.*G13 + x2e.*G23 + x3e)));
gamma1e = exp(A1e);
A2e = (x1e.*tau12*G12 + x3e.*tau32*G32)./(x1e.*G12 + x2e + x3e.*G32) + ((x1e.*G21./(x1e + x2e.*G21 + x3e.*G31)).*(tau12 - (x2e.*tau21*G21 + x3e.*tau31*G31)./(x1e + x2e.*G21 + x3e.*G31))) + ((x2e./(x1e.*G12 + x2e + x3e.*G32)).*(-(x1e.*tau12*G12 + x3e.*tau32*G32)./(x1e.*G12 + x2e + x3e.*G32))) + ((x3e.*G23./(x1e.*G13 + x2e.*G23 + x3e)).*(tau32 - (x1e.*tau13*G13 + x2e.*tau23*G23)./(x1e.*G13 + x2e.*G23 + x3e)));
gamma2e = exp(A2e);
A3e = (x1e.*tau13*G13 + x2e.*tau23*G23)./(x1e.*G13 + x2e.*G23 + x3e) + ((x1e.*G21./(x1e + x2e.*G21 + x3e.*G31)).*(tau13 - (x2e.*tau21*G21 + x3e.*tau31*G31)./(x1e + x2e.*G21 + x3e.*G31))) + ((x2e.*G32./(x1e.*G12 + x2e + x3e.*G32)).*(tau23 - (x1e.*tau12*G12)./(x1e.*G12 + x2e + x3e.*G32))) + ((x3e./(x1e.*G13 + x2e.*G23 + x3e)).*(-(x1e.*tau13*G13 + x2e.*tau23*G23)./(x1e.*G13 + x2e.*G23 + x3e)));
gamma3e = exp(A3e);
A1r = (x2r.*tau21*G21 + x3r.*tau31*G31)./(x1r + x2r.*G21 + x3r.*G31) + ((x1r./(x1r + x2r.*G21 + x3r.*G31)).*(-(x2r.*tau21*G21 + x3e.*tau31*G31)./(x1r + x2r.*G21 + x3r.*G31))) + ((x2r.*G12)./(x1r.*G12 + x2r + x3e.*G32)).*(tau21 - ((x1r.*tau12*G12 + x3r.*tau32*G32)./(x1r.*G12 + x2r + x3r.*G32))) + ((x3r.*G13./(x1r.*G13 + x3r)).*(tau31 - (x1r.*tau13*G13 + x2r.*tau23*G23)./(x1r.*G13 + x2r.*G23 + x3r)));
gamma1r = exp(A1r);
A2r = (x1r.*tau12*G12 + x3r.*tau32*G32)./(x1r.*G12 + x2r + x3r.*G32) + ((x1r.*G21./(x1r + x2r.*G21 + x3r.*G32)).*(tau12 - (x2r.*tau21.*G21 + x3r.*tau31*G31)./(x1r + x2r.*G21 + x3r.*G31))) + ((x2r./(x1r.*G12 + x2r + x3r.*G32)).*(-(x1r.*tau12*G12 + x3r.*tau32*G32)./(x1r.*G12 + x2r + x3r.*G32))) + ((x3r.*G23./(x1r.*G13 + x2r.*G23 + x3r)).*(tau32 - (x1r.*tau13*G13 + x2r.*tau23*G23 + x3r)));
gamma2r = exp(A2r);
A3r = (x1r.*tau13*G13 + x2r.*tau23*G23)./(x1r.*G13 + x2r.*G23 + x3r) + ((x1r.*G31./(x1r + x2r.*G21 + x3r.*G31)).*(tau13 - (x2r.*tau21*G21 + x3r.*tau31*G31)./(x1r + x2r.*G21 + x3r.*G31))) + ((x2r.*G32./(x1r.*G12 + x2r + x3r.*G32)).*(tau23 - (x1r.*tau12*G12)./(x1r.*G12 + x2r + x3r.*G32))) + ((x3r./(x1r.*G13 + x2r.*G23 + x3r)).*(-(x1r.*tau13*G13 + x2r.*tau23*G23)./(x1r.*G13 + x2r.*G23 + x3r)));
gamma3r = exp(A3r);
for i = 1:10
A(i)=((x1e(i)*gamma1e(i) - x1r(i)*gamma1r(i))/(x1e(i)*gamma1e(i) + x1r(i)*gamma1r(i))^2) + ((x2e(i)*gamma2e(i) - x2r(i)*gamma2r(i))/(x2e(i)*gamma2e(i) + x2r(i)*gamma2r(i))^2) + ((x3e(i)*gamma3e(i) - x3r(i)*gamma3r(i))/(x3e(i)*gamma3e(i) + x3r(i).*gamma3r(i))^2);
end
F2 = sum(A,'all');
OF2 = matlabFunction(F2)
end

5 件のコメント

Stephen23
Stephen23 2022 年 1 月 25 日
編集済み: Stephen23 2022 年 1 月 25 日
According to the ga documentation "Write the objective function to accept a row vector of length nvars and return a scalar value." Instead of returning a numeric value as the documentation requires your function returns a function handle (i.e. that returned by matlabFunction). I would not expect that to work.
Dario Miric
Dario Miric 2022 年 1 月 25 日
Thanks for the answer. However, how can objective function return numeric value since objective function doesn't have known parameters? Parameters are yet to be determined and so how can objective function give numeric value?
Torsten
Torsten 2022 年 1 月 25 日
編集済み: Torsten 2022 年 1 月 25 日
ga hands numerical test values to OF_A and tries to improve them in the course of the optimization.
So remove the line
syms tau12 tau13 ...
in OF_A and simply set
OF2 = F2
at the end.
And define
OF_A = @{x) OF_2_6H_60C(x(1),x(2),x(3),x(4),x(5),x(6))
instead of your setting.
Dario Miric
Dario Miric 2022 年 1 月 25 日
編集済み: Dario Miric 2022 年 1 月 25 日
Hmm, thanks. I thought I needed to define parameters as symbolic variables because matlab wouldn't know what these are without defining them first in some way. What you said has sense in context of have ga algorithm works in matlab. Can I leave tau's as parameters because it is a lot of work to change every parameter to x?
Torsten
Torsten 2022 年 1 月 26 日
In function OF_2_6H_60C, you can work with the name tau for the variables.

サインインしてコメントする。

回答 (0 件)

カテゴリ

製品

リリース

R2020a

質問済み:

2022 年 1 月 25 日

コメント済み:

2022 年 1 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by