フィルターのクリア

Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

19 ビュー (過去 30 日間)
Hi all, Im working on a code that simulates a stock price over two years and then compares it to a strike price (call options). I want to simulate the stock price 3000 for each starting value (30 to 150), then take the average payout (for that particular staring price) and plot that vs the starting price. The idea is that if the final price is higher than the strike price (starting price) you get a payout equal to the difference between the final and strike price, if not you get 0.
I keep getting an error (Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.) gives the error in the line
for S(1, :) = 30 : 150
Here is my code
r = 0.05;
T = 2;
dt = 1/365;
sigma = sqrt(r);
rng('shuffle')
eta = randn(1, 365*T+1);
n_iterations = 3000;
S = zeros(365*T, n_iterations);
for S(1, :) = 30 : 150
n = 1 : n_iterations;
for k = 1 : 365*T
S(k+1, n) = S(k, n) + S(k, n).*r.*dt + sqrt(dt).*sigma.*S(k, n).*eta(k, n);
end
strike = S(1, :);
price = S((365*T + 1), :);
eurocallreturn(strike, price);
avereturn = mean(eurocallreturn);
hold on
plot(strike, avereturn)
xlabel('Price')
ylabel('Return')
end
function R = eurocallreturn(strike, price)
if price > strike
R = price - strike;
else if price <= strike
R = 0;
end
end
end

採用された回答

Walter Roberson
Walter Roberson 2019 年 9 月 24 日
In MATLAB, the variable immediately after the keyword for must be a simple variable, not indexed in any way -- no () indexing, no {} indexing, no dot indexing. For example it is not permitted to write
for counters.laps = 1 : 20
You will need to rewrite to something like
for Sidx = 30 : 150
S(1, :) = Sidx;
....
end
  2 件のコメント
Dawid Brits
Dawid Brits 2019 年 9 月 24 日
I think that did the trick, however its giving me a different error
Index in position 2 exceeds array bounds (must not exceed 731).
Error in assig5part2b (line 16)
S(k+1, n) = S(k, n) + S(k, n).*r.*dt + sqrt(dt).*sigma.*S(k, n).*eta(k, n);
is this because I'm initializing the vector in the wrong size?
Thanks in advance
Walter Roberson
Walter Roberson 2019 年 9 月 24 日
It is a problem with you you initialize and use eta which I answered in your other Question.

サインインしてコメントする。

その他の回答 (2 件)

Manoj Shukla
Manoj Shukla 2020 年 4 月 1 日
Hello,
Please help me.
Email Id:- rsmanojshukla@gmail.com
0.3764
0.5673
0.1168
0.1535
0.6345
Train Accuracy: 83.050847
Expected accuracy (with lambda = 1): 83.1 (approx)
>>
>>
>> submit()
'parts' requires one of the following:
Automated Driving Toolbox
Navigation Toolbox
Robotics System Toolbox
Sensor Fusion and Tracking Toolbox
Error in submitWithConfiguration (line 4)
parts = parts(conf);
Error in submit (line 40)
submitWithConfiguration(conf);
Thanks & Regards,
Manoj
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 1 日
submitWithConfiguration contains code in which the name parts is used both for a function and as a variable name. With recent changes to MATLAB, MATLAB cannot locate the function. The repair for this is to change submitWithConfiguration to use a different variable name instead of parts .

サインインしてコメントする。


Merve Özkanat
Merve Özkanat 2022 年 9 月 27 日
Hi, I get the same error. Could you help me?
global a b t E nu
GP1=(-a/sqrt(3),-b/sqrt(3));
GP2=(a/sqrt(3),-b/sqrt(3));
GP3=(a/sqrt(3),b/sqrt(3));
GP4=(-a/sqrt(3),b/sqrt(3));

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by