Prev: Symbolic toolbox solution
Next: Finding a root
From: Philip M on 9 Jul 2010 07:53 Hey guys, I'm new to Matlab and have a (little) question. I want to find the root of this function: 0.247=-sum(pvdef)*0.05+sum(pvprot) pvdef and pvprot depend on rho. r=0.02 ; R=0.4 ; A=0.001 ; B=0.03 ; x= -norminv(A/(1-R)); z= -norminv(B/(1-R)); k=1:20 ; stetverzinsung=exp(-r*(k/4)); c=norminv(1-exp((-0.00625*k)/4)); sigma=[1 -(1-rho)^0.5; -(1-rho)^0.5 1]; for i=1:20, p(i)=mvncdf([x,c(i)], 0, sigma); q(i)=mvncdf([z,c(i)], 0, sigma); E(i)=((1-R)/(B-A))*(p(i)-q(i)); EL(i+1)=E(i); end EL(1)=0; for i=2:21, pvprot(i-1)=stetverzinsung(i-1)*(EL(i)-EL(i-1)); pvdef(i-1)=0.25*stetverzinsung(i-1)*(1-EL(i)); end I want to calculate the value of rho that makes my function zero. But actually I don't know how to do this because of the for loop. Thanks for your help! Philip
From: John D'Errico on 9 Jul 2010 08:51 "Philip M" <philip.messow(a)gmx.de> wrote in message <i17320$rsd$1(a)fred.mathworks.com>... > Hey guys, > > I'm new to Matlab and have a (little) question. I want to find the root of this function: > > 0.247=-sum(pvdef)*0.05+sum(pvprot) > > pvdef and pvprot depend on rho. > > r=0.02 ; > R=0.4 ; > A=0.001 ; > B=0.03 ; > x= -norminv(A/(1-R)); > z= -norminv(B/(1-R)); > k=1:20 ; > stetverzinsung=exp(-r*(k/4)); > c=norminv(1-exp((-0.00625*k)/4)); > sigma=[1 -(1-rho)^0.5; -(1-rho)^0.5 1]; > for i=1:20, > p(i)=mvncdf([x,c(i)], 0, sigma); > q(i)=mvncdf([z,c(i)], 0, sigma); > E(i)=((1-R)/(B-A))*(p(i)-q(i)); > EL(i+1)=E(i); > end > EL(1)=0; > for i=2:21, > pvprot(i-1)=stetverzinsung(i-1)*(EL(i)-EL(i-1)); > pvdef(i-1)=0.25*stetverzinsung(i-1)*(1-EL(i)); > end > > I want to calculate the value of rho that makes my function zero. But actually I don't know how to do this because of the for loop. > Why is the presence of a loop a problem? Surely you can write a function, then pass that function name into fzero to solve for? WTP? John
From: Philip M on 9 Jul 2010 09:30 "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i175vn$k$1(a)fred.mathworks.com>... > "Philip M" <philip.messow(a)gmx.de> wrote in message <i17320$rsd$1(a)fred.mathworks.com>... > > Hey guys, > > > > I'm new to Matlab and have a (little) question. I want to find the root of this function: > > > > 0.247=-sum(pvdef)*0.05+sum(pvprot) > > > > pvdef and pvprot depend on rho. > > > > r=0.02 ; > > R=0.4 ; > > A=0.001 ; > > B=0.03 ; > > x= -norminv(A/(1-R)); > > z= -norminv(B/(1-R)); > > k=1:20 ; > > stetverzinsung=exp(-r*(k/4)); > > c=norminv(1-exp((-0.00625*k)/4)); > > sigma=[1 -(1-rho)^0.5; -(1-rho)^0.5 1]; > > for i=1:20, > > p(i)=mvncdf([x,c(i)], 0, sigma); > > q(i)=mvncdf([z,c(i)], 0, sigma); > > E(i)=((1-R)/(B-A))*(p(i)-q(i)); > > EL(i+1)=E(i); > > end > > EL(1)=0; > > for i=2:21, > > pvprot(i-1)=stetverzinsung(i-1)*(EL(i)-EL(i-1)); > > pvdef(i-1)=0.25*stetverzinsung(i-1)*(1-EL(i)); > > end > > > > I want to calculate the value of rho that makes my function zero. But actually I don't know how to do this because of the for loop. > > > > Why is the presence of a loop a problem? > > Surely you can write a function, then pass that function > name into fzero to solve for? > > WTP? > > John And how do I have to set up this? My problem is that I don't know how to tell Matlab to using the loops for calculating pvdef and pvprot. Because first of all the equation I want to solve does not depend on rho directly. Do I have to set up pvprot and pvdef as functions of rho?
From: Steven Lord on 9 Jul 2010 09:52 "Philip M" <philip.messow(a)gmx.de> wrote in message news:i1789f$n9h$1(a)fred.mathworks.com... > "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message > <i175vn$k$1(a)fred.mathworks.com>... >> "Philip M" <philip.messow(a)gmx.de> wrote in message >> <i17320$rsd$1(a)fred.mathworks.com>... >> > Hey guys, >> > >> > I'm new to Matlab and have a (little) question. I want to find the root >> > of this function: >> > >> > 0.247=-sum(pvdef)*0.05+sum(pvprot) >> > >> > pvdef and pvprot depend on rho. >> > *snip code* >> > I want to calculate the value of rho that makes my function zero. But >> > actually I don't know how to do this because of the for loop. >> >> Why is the presence of a loop a problem? Surely you can write a function, >> then pass that function >> name into fzero to solve for? >> >> WTP? >> >> John > > > And how do I have to set up this? My problem is that I don't know how to > tell Matlab to using the loops for calculating pvdef and pvprot. Because > first of all the equation I want to solve does not depend on rho directly. > Do I have to set up pvprot and pvdef as functions of rho? You will need to write a function that accepts rho as input and calculates the value of: f(rho) = -sum(pvdef)*0.05+sum(pvprot) - 0.247 FZERO doesn't really care HOW your function evaluates f(rho), all it cares about is that your function accepts a scalar input and returns a scalar output. Your function could do any or all of the following (or more) to generate the output: * Run some MATLAB code * Simulate a Simulink model * Query a database using Database Toolbox * Retrieve information from a physical instrument using Instrument Control Toolbox * Ask the user to perform some sort of experiment ("Elevate the ramp to 23.74 degrees and place the block at the top of the ramp") and enter the result of that experiment using the INPUT function It looks like the script file you posted gets you most of the way there to satisfying FZERO's requirements; you just need to turn it into a function with the appropriate inputs and outputs. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: Philip M on 9 Jul 2010 12:13
If I put down my script file first, I always get the message, that rho does not exist. So do I have to set up sigma, p and q as a function of rho? Even if I define sigma as a function of rho I get the error message. function sigma = funz(rho) sigma=[1 -(1-rho)^0.5; -(1-rho)^0.5 1]; end function F = funy(rho) F=sum(pvprot)-sum(pvdef)*0.05-0.247; end Sorry for bothering you. "Steven Lord" <slord(a)mathworks.com> wrote in message <i179jc$fh2$1(a)fred.mathworks.com>... > > "Philip M" <philip.messow(a)gmx.de> wrote in message > news:i1789f$n9h$1(a)fred.mathworks.com... > > "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message > > <i175vn$k$1(a)fred.mathworks.com>... > >> "Philip M" <philip.messow(a)gmx.de> wrote in message > >> <i17320$rsd$1(a)fred.mathworks.com>... > >> > Hey guys, > >> > > >> > I'm new to Matlab and have a (little) question. I want to find the root > >> > of this function: > >> > > >> > 0.247=-sum(pvdef)*0.05+sum(pvprot) > >> > > >> > pvdef and pvprot depend on rho. > >> > > > *snip code* > > >> > I want to calculate the value of rho that makes my function zero. But > >> > actually I don't know how to do this because of the for loop. > >> > >> Why is the presence of a loop a problem? Surely you can write a function, > >> then pass that function > >> name into fzero to solve for? > >> > >> WTP? > >> > >> John > > > > > > And how do I have to set up this? My problem is that I don't know how to > > tell Matlab to using the loops for calculating pvdef and pvprot. Because > > first of all the equation I want to solve does not depend on rho directly. > > Do I have to set up pvprot and pvdef as functions of rho? > > You will need to write a function that accepts rho as input and calculates > the value of: > > f(rho) = -sum(pvdef)*0.05+sum(pvprot) - 0.247 > > FZERO doesn't really care HOW your function evaluates f(rho), all it cares > about is that your function accepts a scalar input and returns a scalar > output. Your function could do any or all of the following (or more) to > generate the output: > > * Run some MATLAB code > * Simulate a Simulink model > * Query a database using Database Toolbox > * Retrieve information from a physical instrument using Instrument Control > Toolbox > * Ask the user to perform some sort of experiment ("Elevate the ramp to > 23.74 degrees and place the block at the top of the ramp") and enter the > result of that experiment using the INPUT function > > It looks like the script file you posted gets you most of the way there to > satisfying FZERO's requirements; you just need to turn it into a function > with the appropriate inputs and outputs. > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > To contact Technical Support use the Contact Us link on > http://www.mathworks.com > |