I have a function UE=Explicit(S,sigma,r,T,M,K,gamma,N); and I want to plot how the solution depends on gamma where 0.5<=gamma<=1. All of the other parameters are constants. How can I do this?

 채택된 답변

Star Strider
Star Strider 2019년 9월 17일
편집: Star Strider 2019년 9월 17일

0 개 추천

One approach:
gammav = linspace(0.5, 1, 10);
for k = 1:numel(gammav)
UE{k} = Explicit(S,sigma,r,T,M,K,gammav(k),N);
end
I have no idea what ‘Explicit’ is or what it produces, so it could be possible to vectorise this (instead of using the loop) depending on how you wrote the function. I am also saving each iteration to a cell array for that reason.
EDIT —
Plotting it depends on what ‘UE’ is. If ‘Explicit’ produces a scalar, this works:
figure
plot(gammav, [UE{:}])
grid
If it produces vectors or matrices, it will be necessary to use other approaches.

댓글 수: 8

The function looks like this, so I guess I have to make a matrice.
function [U,A] = ExplicitJ(S,sigma,r,T,M,K,gamma,N)
smax=4*K;
deltas=smax/M;
deltat=T/N;
V=zeros(M-1,N);
V(:,N)=max(deltas*[1:M-1]-K,0);
jv=ones(1,M-1)*(1-r*deltat)-deltat/deltas^2*sigma^2.*(deltas*[1:M-1]).^(2*gamma);
jm=1/2*deltat/deltas^2*sigma^2*([1:M-1]*deltas).^(2*gamma)-r*deltat/deltas.*(deltas*[1:M-1])/2;
jp=1/2*deltat/deltas^2*sigma^2*(deltas*[1:M-1]).^(2*gamma)+deltat/deltas*r.*(deltas*[1:M-1])/2;
A1=diag(jv);
A2=diag(jm(1,2:M-1),-1);
A3=diag(jp(1,1:M-2),+1);
A=A1+A2+A3;
b=zeros(M-1,1);
for n=N:-1:2
b(M-1)=jp(1,M-1)*(smax-K*exp(-r*(T-deltat*n)));
V(:,n-1)=A*V(:,n)+b;
end
V=V(:,1);
end
Star Strider
Star Strider 2019년 9월 17일
‘so I guess I have to make a matrice’
Yes, most likely.
What are the other arguments to ‘Explicit’?
What output do you want to save (here ‘UE’ will only be ‘U’, since MATLAB will not return the second output unless you specifically ask for it)?
What do you want in terms of a plot? If ‘UE’ is a matrix, you will likely get stacked mesh or surf plots.
Amanda Hoxell
Amanda Hoxell 2019년 9월 17일
편집: Amanda Hoxell 2019년 9월 17일
The main program is:
S=14;
sigma=0.25;
r=0.1;
T=0.5;
gamma=1;
K=15;
N=10000;
M=200;
deltas=4*K/M;
[UE,A]=Explicit(S,sigma,r,T,M,K,gamma,N)
plot(UE, 'r')
where plot(UE, 'r') plots the solution. I want the same plot but when varying gamma.
Star Strider
Star Strider 2019년 9월 17일
I just noticed that ‘U’ is not actually assigned anywhere in ‘ExplicitJ’. Should ‘V’ be ‘U’?
Also, since the plot calls ‘Explicit’, did you post the correct function?
Amanda Hoxell
Amanda Hoxell 2019년 9월 17일
편집: Amanda Hoxell 2019년 9월 17일
Yes you are right, should be . Sorry for the confusion.
Yes, is the correct function. The plot looks like this
fig.jpg
This works, however, ‘UE’ is uniformly 0 for all values of ‘gammav’.
gammav = linspace(0.5, 1, 10);
for k = 1:numel(gammav)
[UE(:,k),A]=Explicit(S,sigma,r,T,M,K,gammav(k),N);
end
figure
plot(gammav, UE, '.r')
This code runs without errors.
Amanda Hoxell
Amanda Hoxell 2019년 9월 17일
Thank you!!
Star Strider
Star Strider 2019년 9월 17일
My pleasure!
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2019년 9월 17일

1 개 추천

Using gamma as the name of a variable is a bad idea. Regardless,...
Where is the problem? Just substitute a range of values for gamma. Save the solution for each gamma. Then plot, with gamma on the x axis, and your solution on the y axis. WTP?
You can even use tools like fimplicit, which will do the hard work for you. For example,
f = @(gam,y) gam.^2 - y.^2 - 2*gam.*y.^3 + gam.*y + 1;
fimplicit(f)
xlabel 'gam'
ylabel 'y'
grid on
untitled.jpg

댓글 수: 1

The problem is that I am new to Matlab. I tried the following but it did not work..
gamma_all = 0.5:0.1:1;
y = zeros(1,length(gamma_all));
for i = 1:length(gamma_all)
gamma = gamma_all(i);
y(i) = Explicit(S,sigma,r,T,M,K,gamma,N);
end
plot(gamma_all,y)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

질문:

2019년 9월 17일

댓글:

2019년 9월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by