How to fix integral calculation error when the function handle become non-symbolic?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to optimize a problem using genetic algorithm. I write below code and use ga for optimization. It dose work and find x as answer:
SOC_new=100;
DOD_new=0;
t=1
x=sym('x');
SOC=@(x)(SOC_new);
DOD=@(x)(DOD_new);
Uc=@(x)(U0-g*DOD(x)+R_c*x./CN+R_c*M_c*x./CN*SOC(x)/(Cc-SOC(x)));
I_gas=@(x)(CN/100*I_gas0*exp(cu*(Uc(x)-U_gas0)+cT*(T-Tgas0)));
I_ch=@(x)((x-I_gas(x))/CN);
SOC=@(x)(SOC(x)+quadgk(I_ch(x),0,t)); %%or @(x)(SOC(x)+integral(I_ch(x),0,t)) or @(x)(SOC(x)+integral(I_ch(x),0,t,'ArrayValued',true)) or @(x)(SOC(x)+int(I_ch(x),x,0,t))==> "" all of these commands work""
DOD=@(x)(1-SOC(x));
P_c=@(x)(Uc(x)*x);
After optimization, when i want to see the value of SOC i use below because x is found:
>>SOC(x)
But i got the following error:
Error using quadgk (line 96)
First input argument must be a function handle.
or this:
Error using integral (line 96)
First input argument must be a function handle.
or this:
Undefined function 'int' for input arguments of type 'double'.
While i want to see the value of I_ch, i got no error:
>> I_ch(x)
ans =
-4.2614e+06
How can i fix the integral error? I'll be grateful for any help.
0 个评论
采纳的回答
Walter Roberson
2016-12-25
SOC=@(x) SOC_new * ones(size(x));
DOD=@(x) DOD_new * ones(size(x));
Note: you have
SOC=@(x)(SOC(x)+quadgk(I_ch(x),0,t));
DOD=@(x)(1-SOC(x));
Please use different variables than SOC and DOD when you define these. Although what you coded is legal, it confuses the readers, and it is likely to fail if you have a loop. Additional variable names are nearly free; brain cells trying to figure out self-referential anonymous functions are not.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!