# Thread Subject: Problem using fmincon

 Subject: Problem using fmincon From: Per Wehl Date: 24 Apr, 2012 08:29:07 Message: 1 of 7 Hi, We are two students currently working on our masters thesis from Sweden. As a part of our project we need to use minimize five variables regarding risk neutral probability density functions extracted from option prices. Never having used MATLAB before we have encountered some difficulties and would greatly appreciate any help! We are copying the code below! x(1) x(2) x(3) x(4) and x(5) are the parameters to be minimized. chat phat kc kp dtmc dtmp and F are variables we have imported into 1 column matrices from excel. c and p also needs to be calculated in MATLAB. When using the optimtool we get an error saying that the length of the upper bound > the length of x and the optimization terminates. The goal is to get the five values for x. Here goes the code: function [x0] = myfun1(x0)     function f = myfun1(x0,c,chat,p,phat,F)         f = ((sum(c-chat,0,14589)^2)+((sum(p-phat)^2))+((x0(5).*(exp(x0(1)+.5.*(x0(3).^2)))+(1-x0(5)).*(exp(x0(2)+.5.*x0(4).^2))-F)^2));                  function c = tcall (r, dtmc, q1, q2, q3, q4, kc)             c=exp(-r.*dtmc).*(x0(5)).*(exp(x0(1)+.5.*(x0(3).^2).*(q1)-kc.*(q2)))+(1-x0(5)).*(exp(x0(2)+.5.*(x0(4).^2).*(q3)-kc.*(q4)));             myfun1=@tcall;                      function p = tput (r, dtmp, q1, q2, q3, q4, kp)             p=exp(-r.*dtmp).*(x0(5).*(exp(x0(1)+.5.*(x0(3).^2).*((-1)*q1)-kp.*((-1).*q2))))+(1-x0(5)).*(exp(x0(2)+.5.*(x0(4).^2).*((-1).*q3)-kp.*((-1).*q4)));             myfun1=@tput;                          function q1 = qett (d1)                 q1=cdf('normal',d1,0,1);                 tput=@qett;                 tcall=@qett;                              function q2 = qtva (d2)                 q2=cdf('normal',d2,0,1);                 tput=@qtva;                 tcall=@qtva;                              function q3 = qtre (d3)                 q3=cdf('normal',d3,0,1);                 tput=@qtre;                 tcall=@qtre;                              function q4 = qfyra (d4)                 q4=cdf('normal',d4,0,1);                 tput=@qfyra;                 tcall=@qfyra;                                                  function d2 = dtva (d1, x0)                     d2=d1-x0(3);                     qett=@dtva;                                            function d4 = dfyra (d3, x0)                     d4=d3-x0(4);                     qfyra=@dfyra;                                                           function d1 = dett (k, x0)                         d1=(-log(k)+x0(1)+x0(3).^2)./x0(3);                         dtva=@dett;                                                           function d3 = dtre (k, x0)                         d3=(-log(k)+x0(2)+x0(4).^2)./x0(4);                         dfyra=@dtre;                     end                     end                 end                 end             end             end             end             end         end         end     end end Any help is much appreciated! Thanks in advance!
 Subject: Problem using fmincon From: pietro Date: 24 Apr, 2012 09:01:09 Message: 2 of 7 "Per Wehl" wrote in message ... > Hi, > We are two students currently working on our masters thesis from Sweden. > As a part of our project we need to use minimize five variables regarding risk neutral probability density functions extracted from option prices. > > Never having used MATLAB before we have encountered some difficulties and would greatly appreciate any help! > > We are copying the code below! > x(1) x(2) x(3) x(4) and x(5) are the parameters to be minimized. > chat phat kc kp dtmc dtmp and F are variables we have imported into 1 column matrices from excel. c and p also needs to be calculated in MATLAB. > > When using the optimtool we get an error saying that the length of the upper bound > the length of x and the optimization terminates. > > The goal is to get the five values for x. > > Here goes the code: > > function [x0] = myfun1(x0) > > function f = myfun1(x0,c,chat,p,phat,F) > f = ((sum(c-chat,0,14589)^2)+((sum(p-phat)^2))+((x0(5).*(exp(x0(1)+.5.*(x0(3).^2)))+(1-x0(5)).*(exp(x0(2)+.5.*x0(4).^2))-F)^2)); > > function c = tcall (r, dtmc, q1, q2, q3, q4, kc) > c=exp(-r.*dtmc).*(x0(5)).*(exp(x0(1)+.5.*(x0(3).^2).*(q1)-kc.*(q2)))+(1-x0(5)).*(exp(x0(2)+.5.*(x0(4).^2).*(q3)-kc.*(q4))); > myfun1=@tcall; > > function p = tput (r, dtmp, q1, q2, q3, q4, kp) > p=exp(-r.*dtmp).*(x0(5).*(exp(x0(1)+.5.*(x0(3).^2).*((-1)*q1)-kp.*((-1).*q2))))+(1-x0(5)).*(exp(x0(2)+.5.*(x0(4).^2).*((-1).*q3)-kp.*((-1).*q4))); > myfun1=@tput; > > function q1 = qett (d1) > q1=cdf('normal',d1,0,1); > tput=@qett; > tcall=@qett; > > function q2 = qtva (d2) > q2=cdf('normal',d2,0,1); > tput=@qtva; > tcall=@qtva; > > function q3 = qtre (d3) > q3=cdf('normal',d3,0,1); > tput=@qtre; > tcall=@qtre; > > function q4 = qfyra (d4) > q4=cdf('normal',d4,0,1); > tput=@qfyra; > tcall=@qfyra; > > > function d2 = dtva (d1, x0) > d2=d1-x0(3); > qett=@dtva; > > > function d4 = dfyra (d3, x0) > d4=d3-x0(4); > qfyra=@dfyra; > > > function d1 = dett (k, x0) > d1=(-log(k)+x0(1)+x0(3).^2)./x0(3); > dtva=@dett; > > > function d3 = dtre (k, x0) > d3=(-log(k)+x0(2)+x0(4).^2)./x0(4); > dfyra=@dtre; > end > end > end > end > end > end > end > end > end > end > end > end > > Any help is much appreciated! > > Thanks in advance! Hi, as it is reported from matlab the lower bound array is smaller than x. On optitool gui-> go on files-> generate code and report here the code. bye Pietro
 Subject: Problem using fmincon From: Per Wehl Date: 24 Apr, 2012 09:14:06 Message: 3 of 7 "pietro " wrote in message > Hi, > > as it is reported from matlab the lower bound array is smaller than x. On optitool gui-> go on files-> generate code and report here the code. > > bye > > Pietro Hi! Thanks for your input! Heres what we get when following these steps. function [x,fval,exitflag,output,lambda,grad,hessian] = untitled(x0,lb,ub) %% This is an auto generated MATLAB file from Optimization Tool. %% Start with the default options options = optimset; %% Modify options setting options = optimset(options,'Display', 'off'); options = optimset(options,'Algorithm', 'interior-point'); [x,fval,exitflag,output,lambda,grad,hessian] = ... fmincon(@myfun1,x0,[],[],[],[],lb,ub,[],options); Hope this helps! /Per
 Subject: Problem using fmincon From: Steven_Lord Date: 24 Apr, 2012 13:49:33 Message: 4 of 7 "Per Wehl" wrote in message news:jn5o8j\$2q7\$1@newscl01ah.mathworks.com... > Hi, > We are two students currently working on our masters thesis from Sweden. > As a part of our project we need to use minimize five variables regarding > risk neutral probability density functions extracted from option prices. > > Never having used MATLAB before we have encountered some difficulties and > would greatly appreciate any help! > > We are copying the code below! > x(1) x(2) x(3) x(4) and x(5) are the parameters to be minimized. Okay, so all of the following inputs to FMINCON should be consistent with a five-parameter problem: * your objective function should accept a vector with 5 elements and return a scalar * your initial guess should be a vector with 5 elements * A and Aeq should have 5 columns (or be empty, if you don't have that kind of linear constraint.) * lb and ub should be vectors with 5 elements (or empty, if you don't need/want to specify bounds.) * nonlcon should accept a vector with five elements and return two outputs * any of the options you set in your options structure should be consistent with a 5-element parameter vector > chat phat kc kp dtmc dtmp and F are variables we have imported into 1 > column matrices from excel. c and p also needs to be calculated in MATLAB. > > When using the optimtool we get an error saying that the length of the > upper bound > the length of x and the optimization terminates. So your ub input is a vector with more than 5 elements. > The goal is to get the five values for x. > > Here goes the code: > > function [x0] = myfun1(x0) > > function f = myfun1(x0,c,chat,p,phat,F) I'm not sure what exactly you're doing here; that first function declaration line shouldn't be there. *snip* -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com
 Subject: Problem using fmincon From: Per Wehl Date: 24 Apr, 2012 20:15:15 Message: 5 of 7 Thanks for the reply and your advice! We’ve now updated the code according to your descriptions, however we now get another error: “Error using runmyfun1 (line 3) Not enough input arguments.” At line 3 it’s the following code; [x,fval] = fmincon(@myfun1,x0,A,b,[],[],lb,ub,options); x0 is = [3,0.1, 3,0.25,0.5] What could this error depend on? Thanks in advance for your help! Below follows the whole code as it is in our runmyfun1.m file: function [x,fval] = runmyfun1(x0) [x,fval] = fmincon(@myfun1,x0,A,b,[],[],lb,ub,options); function f = myfun1(x,cdev,F,pdev)      f = ((cdev)+(pdev))+((x(5).*(exp(x(1)+.5.*(x(3).^2)))+(1-x(5)).*(exp(x(2)+.5.*x(4).^2))-F)^2);      for m=1;length(chat);          cdev=(c(m)-chat(m)).^2;          sum(cdev);      for n=0,length(phat);          pdev=(p(n)-phat(n)).^2;          sum(pdev);      end      end          function c = tcall (r, dtmc, q1, q2, q3, q4, kc)          c=exp(-r.*dtmc).*(x(5)).*(exp(x(1)+.5.*(x(3).^2).*(q1)-kc.*(q2)))+(1-x(5)).*(exp(x(2)+.5.*(x(4).^2).*(q3)-kc.*(q4)));          myfun1=@tcall;                 function p = tput (r, dtmp, q1, q2, q3, q4, kp)          p=exp(-r.*dtmp).*(x(5).*(exp(x(1)+.5.*(x(3).^2).*((-1)*q1)-kp.*((-1).*q2))))+(1-x(5)).*(exp(x(2)+.5.*(x(4).^2).*((-1).*q3)-kp.*((-1).*q4)));          myfun1=@tput;                  function q1 = qett (d1)              q1=cdf('normal',d1,0,1);              tput=@qett;              tcall=@qett;                      function q2 = qtva (d2)              q2=cdf('normal',d2,0,1);              tput=@qtva;              tcall=@qtva;                      function q3 = qtre (d3)              q3=cdf('normal',d3,0,1);              tput=@qtre;              tcall=@qtre;                      function q4 = qfyra (d4)              q4=cdf('normal',d4,0,1);              tput=@qfyra;              tcall=@qfyra;                                     function d2 = dtva (d1, x)                  d2=d1-x(3);                  qett=@dtva;                               function d4 = dfyra (d3, x)                  d4=d3-x(4);                  qfyra=@dfyra;                                              function d1 = dett (k, x)                      d1=(-log(k)+x(1)+x(3).^2)./x(3);                      dtva=@dett;                                              function d3 = dtre (k, x)                      d3=(-log(k)+x(2)+x(4).^2)./x(4);                      dfyra=@dtre;                  end                  end              end              end          end          end          end          end      end      end end end
 Subject: Problem using fmincon From: Per Wehl Date: 26 Apr, 2012 13:33:27 Message: 7 of 7 Hi! Thanks for your help! Indeed as you thought we are first time MATLAB users. We've had some progress with our code and it now process the minimum x values for x1-x5 as intended using fmincon. Another issue has however risen. To calculate c and p from "function c" and "function p" we need values from the cdf command in MATLAB, and these values depend on the variables d1-d4. d1-d4 are however different for function c and function p. Hence we would need two sets of the code calculating the process going from d1-d4 to q1-q4. One where we use kc as input, and one with kp. When we try to add these functions MATLAB says we exceed the maximum limit of nested functions. Is there a smart way around this, how do you suggest we go about this issue? Thanks in advance and best regards /Per Wehl Full code below:         function c = tcall (r, dtmc, q1, q2, q3, q4, kc)             c=(exp(-r.*dtmc).*((x(5).*(exp(x(1)+(.5.*x(3).^2))).*(q1)-(kc.*(q2)))+((1-x(5)).*(exp(x(2)+(0.5.*x(4).^2))).*(q3)-(kc.*(q4)))));             cdev=@tcall;                        %pdev=((p-phat).^2);             whos c             whos chat             cdev=((c-chat).^2); % function p = tput (r, dtmp, q1, q2, q3, q4, kp) % p=(exp(-r.*dtmp).*((x(5).*(-exp(x(1)+(.5.*x(3).^2))).*(1-q1)+(kp.*(1-q2)))+((1-x(5)).*(-exp(x(2)+(0.5.*x(4).^2))).*(1-q3)+(kp.*(1-q4))))); % pdev=@tput;                           function q1 = qett (d1)                  q1=cdf('normal',d1,0,1);                 % tput=@qett;                  tcall=@qett;                                function q2 = qtva (d2)                  q2=cdf('normal',d2,0,1);                % tput=@qtva;                  tcall=@qtva;                               function q3 = qtre (d3)                  q3=cdf('normal',d3,0,1);                 % tput=@qtre;                  tcall=@qtre;                               function q4 = qfyra (d4)                  q4=cdf('normal',d4,0,1);                  %tput=@qfyra;                  tcall=@qfyra;                                                   function d2 = dtva (d1, x)                      d2=d1-x(3);                      qett=@dtva;                                              function d4 = dfyra (d3, x)                      d4=d3-x(4);                      qfyra=@dfyra;                                                      function d1 = dett (kc, x)                          d1=(-log(kc)+x(1)+(x(3).^2))./x(3);                          dtva=@dett;                                       function d3 = dtre (kc, x)                          d3=(-log(kc)+x(2)+(x(4).^2))./x(4);                          dfyra=@dtre;

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.