How to fix integral calculation error when the function handle become non-symbolic?
1 view (last 30 days)
Show older comments
rasoul rad
on 25 Dec 2016
Commented: rasoul rad
on 27 Dec 2016
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 Comments
Accepted Answer
Walter Roberson
on 25 Dec 2016
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.
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!