assign value and plot outside the loop

1 次查看(过去 30 天)
kingsley
kingsley 2018-2-27
回答: ag 2025-5-8
function [call_value]=different_k(S, r, sigma, t, steps)
call_value=zeros(226,1);
for i=75:300
call_value(i-74,1) =european_call_binomial(S, i, r, sigma, t, steps);
%plot(call_value(i-74,1),i);
end
x=75:1:300;
y=call_value(1:1,1);
plot(x,y)
I 'm trying to assign the value to call_value. However, the result says call_value has not been defined. Did I made a in call_value(i-74,1)? how should I fix this ?

回答(1 个)

ag
ag 2025-5-8
Hi Kingsley,
The variable "call_value" is defined inside the function "different_k", and therefore, accessing it outside the scope of function "different_k" leads to the error "Unrecognized function or variable 'call_value'".
Below is a modified version of your code:
x=75:1:300;
% ensure to define the variables passed into the function different_k
call_value = different_k(S, r, sigma, t, steps);
plot(x, call_value);
function [call_value]=different_k(S, r, sigma, t, steps)
call_value=zeros(226,1);
for i=75:300
% ensure to provide the function definition of european_call_binomial
call_value(i-74,1) =european_call_binomial(S, i, r, sigma, t, steps);
end
end
For more details, please refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/function.html#:~:text=Function%20with%20One%20Output
ope this helps!

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by