Prev: Symbolic toolbox solution
Next: Finding a root
From: Walter Roberson on 9 Jul 2010 13:39 Philip M wrote: > function F = funy(rho) > > F=sum(pvprot)-sum(pvdef)*0.05-0.247; > > end If pvprot and pvdef depend upon rho (earlier posting), then why does your funy not use rho at all in its calculations?
From: dpb on 9 Jul 2010 13:40 Philip M wrote: ....[top posting repaired...please don't; makes hard conversation follow]... > "Steven Lord" <slord(a)mathworks.com> wrote in message > <i179jc$fh2$1(a)fred.mathworks.com>... .... >> 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: .... >> 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. .... > 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 You have to have any and all variables required in a function in scope in the function somehow, yes. funz() returns sigma given rho (altho I would suggest something more like function sigma = funz(rho) tmp = sqrt(1-rho); sigma=[1 -tmp; -tmp 1]; end as a stylistic change, that's not the problem. The problem is the function funy(rho) doesn't refer to rho but to pvprot and pvdef which aren't passed to it. --
From: dpb on 9 Jul 2010 14:38 dpb wrote: > Philip M wrote: > ...[top posting repaired...please don't; makes hard conversation follow]... > >> "Steven Lord" <slord(a)mathworks.com> wrote in message >> <i179jc$fh2$1(a)fred.mathworks.com>... > ... > >>> 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 >>> .... >>> 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. f = function func(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)); tmp = sqrt(1-rho0; sigma = [1 -tmp; -tmp 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 f = -sum(pvdef)*0.05+sum(pvprot) - 0.247; end Doesn't that do it? I didn't read it carefully to see if the loops could be vectorized or anything... --
From: Philip M on 9 Jul 2010 15:18 I put it down like this... function f = func(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)); tmp = sqrt(1-rho); sigma = [1 -tmp; -tmp 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 f = -sum(pvdef)*0.05+sum(pvprot) - 0.247; end fzero(func, 0) ??? Input argument "rho" is undefined. Error in ==> func at 12 tmp = sqrt(1-rho); If I change tmp = sqrt(1-rho); to tmp = @(rho) ... I get this error message >> fzero(func, 0) ??? Undefined function or method 'uminus' for input arguments of type 'function_handle'. Error in ==> func at 13 sigma = [1 -tmp; -tmp 1]; I was trying it with nested functions, but it does not work at all... dpb <none(a)non.net> wrote in message <i17qfj$s9a$1(a)news.eternal-september.org>... > dpb wrote: > > Philip M wrote: > > ...[top posting repaired...please don't; makes hard conversation follow]... > > > >> "Steven Lord" <slord(a)mathworks.com> wrote in message > >> <i179jc$fh2$1(a)fred.mathworks.com>... > > ... > > > >>> 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 > >>> > ... > > >>> 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. > > f = function func(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)); > tmp = sqrt(1-rho0; > sigma = [1 -tmp; -tmp 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 > > f = -sum(pvdef)*0.05+sum(pvprot) - 0.247; > end > > Doesn't that do it? > > I didn't read it carefully to see if the loops could be vectorized or > anything... > --
From: Steven Lord on 9 Jul 2010 15:22
"Philip M" <philip.messow(a)gmx.de> wrote in message news:i17slc$f46$1(a)fred.mathworks.com... >I put it down like this... *snip code for function func* > fzero(func, 0) > ??? Input argument "rho" is undefined. You're almost there. This FZERO call actually calls func with 0 inputs and attempts to pass the output from that call into FZERO as the first input. [This could be useful if you had a function that returned a function handle as its output.] That's not what you want; you want to pass _a function handle to func_ into FZERO as the first input, so that FZERO can invoke func with a scalar input when it needs to do so. rhovalue = fzero(@func, 0) -- 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 |