0
clc; clearvars; clear all;
syms T; syms E; syms v1; syms v2; syms v3;
assume(v1>0 & v1<50000);
assume(v2>0 & v2<50000);
assume(v3>0 & v3<60000);
b = 10/60;
fun = int(exp(-E/(8.314*T)),T,300,T);
s1 = 175.6 * 10^3;
fun11 = (1/(v1*sqrt(2*pi)))* exp(- ((E-s1)^2)/(2*v1^2));
a1 = 10^14.52;
fun12 = int(exp((-a1/b)*fun)*fun11,E,s1-3*v1,s1+3*v1);
alpha1 = 1 - fun12;
s2 = 185.4 * 10^3;
fun21 = (1/(v2*sqrt(2*pi)))* exp(- ((E-s2)^2)/(2*v2^2));
a2 = 10^13.64;
fun22 = int(exp((-a2/b)*fun)*fun21,E,s2-3*v2,s2+3*v2);
alpha2 = 1 - fun22;
s3 = 195.4 * 10^3;
fun31 = (1/(v3*sqrt(2*pi)))* exp(- ((E-s3)^2)/(2*v3^2));
a3 = 10^13.98;
fun32 = int(exp((-a3/b)*fun)*fun31,E,s3-3*v3,s3+3*v3);
alpha3 = 1 - fun32;
alpha = (alpha1 + alpha2 + alpha3)/3
alphaexp=[0.01134 0.04317];% 0.06494 0.08783 0.17053 0.32533 0.49142 0.55575 0.59242 0.6367 0.678 0.71621 0.75124 0.78442 0.81727];
T = [350 400]; %T = [350:50:1050];
minfunc = (subs(alpha)-alphaexp).^2
error1 = sum(minfunc)
error = matlabFunction(error1)
[xfinal,fval] = fminsearch(@(x)error(x(1),x(2),x(3)),[4300 3500 32000])

The above code produces an error that 'E' is an undefined function or variable. However during all the integrations (fun12, fun22 and fun 32), I have clearly indicated that the integration is over the variable E, with limits containing v1, v2 and v3 respectively. (So E should not even exist in the final error function).

Am I doing some mistake implementing the fminsearch function? Any help will be highly appreciated.

1 Answers1

0

It doesn't even reach the min search. The issue is on the integrations.

Looks like Matlab can't compute a closed form (though the error is not really descriptive).

You can reproduce your this by just doing.

error = matlabFunction(error1)
error(4300, 3500, 32000)

A slow work around is to manually substitute, then numerically compute your solution:

vpa(subs(error1,[v1,v2,v3],[4300 3500 32000]))

The bottleneck is in the substitution. I guess there is some way to combine vpa with matlabFunction to make all this faster, but I don't know about it.

xvan
  • 4,554
  • 1
  • 22
  • 37
  • I understand that using the vpa function will give me the value of the error at a specific [v1,v2,v3] value. However since my aim is to optimize the error1 function for these variables itself, and fminsearch requires a function handle to do the optimization, I don't understand how do I do that? I cannot just keep entering random values of [v1,v2,v3] to see which gives the minimum error. – Vishal Tripathi Apr 06 '16 at 05:00
  • `fminsearch(@(X)vpa(subs(error1,[v1,v2,v3],X)), [4300 3500 32000]) )` run it a couple of times with different seeds to make sure you're not on a local minimum. – xvan Apr 06 '16 at 05:10
  • I am not sure whether my system is slow, but it is taking an awful amount of time to give results (The code is running since your last comment and has not given a solution yet). Am I being impatient, or is it supposed to happen? – Vishal Tripathi Apr 06 '16 at 05:32
  • `subs` is really slow. Try a numeric approach, or print partial results with fminsearch. – xvan Apr 06 '16 at 10:27
  • Sorry for being so ignorant, but how do I print partial results with fminsearch? I mean by using the option ('Display', 'iter'), I would get the error results not the subsequent values... – Vishal Tripathi Apr 09 '16 at 08:38
  • Read about `OutputFcn` on `fminsearch` manual. You'll need a global/persistent variable to check if fval is smaller than the previous step. – xvan Apr 09 '16 at 16:06