How a solution depends on a variable

1 次查看(过去 30 天)
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
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 个评论
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
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 个评论
Amanda Hoxell
Amanda Hoxell 2019-9-17
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by