Error: Array indices must be positive integers or logical values. / no indices in code

Hello,
When running my code I get multiple errors but the error seems to be with a misusage of indices.
My code:
syms P R;
B = 6;
r = @(P) 0.5 * (B - P)^(-0.5);
R_star = @(P) r * P;
R_max = 6;
R_dist = 1 / 6;
c_prime = @(x) x;
expectation = int(@(R) R * R_dist, R, R_star, R_max);
one_minus_cdf = int(@(R) R_dist, R, R_star, R_max);
RHS = @(P) (expectation(P) - c_prime(1)) / (r(P) * one_minus_cdf(P));
to_be_min = @(P) (P - RHS(P))^2;
x0 = 0;
bubble_price = fminsearch(to_be_min, x0);
The errors:
Array indices must be positive integers or logical values.
Error in sym/subsref (line 909)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in
attempt_code_bubble>@(P)(expectation(P)-c_prime(1))/(r(P)*one_minus_cdf(P))
(line 13)
RHS = @(P) (expectation(P) - c_prime(1)) / (r(P) * one_minus_cdf(P));
Error in attempt_code_bubble>@(P)(P-RHS(P))^2 (line 15)
to_be_min = @(P) (P - RHS(P))^2;
Error in fminsearch (line 201)
fv(:,1) = funfcn(x,varargin{:});
Error in attempt_code_bubble (line 18)
bubble_price = fminsearch(to_be_min, x0);
Thank you for your help!

 Accepted Answer

R_star = @(P) r * P;
r is a function handle. You cannot multiply a function handle by a value. You can invoke the function handle and multiply the results.
expectation = int(@(R) R * R_dist, R, R_star, R_max);
That would give you a symbolic expression as a result, not a function
RHS = @(P) (expectation(P) - c_prime(1)) / (r(P) * one_minus_cdf(P));
That would try to index the symbolic expression at the input P. If P were symbolic you would get the error message you are seeing.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!