Please help I am encountering the following error: Error using Fuzzy>@(I_​Htm,Ft)mea​n(Ft(I_Htm​)) Too many input arguments. Error in Fuzzy>@(fQ​)sum((delt​a*exp(-r*(​tm-t)).*E_​Q.

6 views (last 30 days)
% Define the parameters of the model
dTm = 0.1; % Mean-reversion speed parameter
a0 = 0.5; % Fourier series coefficient
a1 = 0.2; % Fourier series coefficient
ac = [0.3, 0.4, 0.1]; % Fourier series coefficients
as = [0.1, 0.2, 0.3]; % Fourier series coefficients
K = 3; % Number of terms in the Fourier series
Tm = 25.0; % Terminal condition parameter
% Define the grid parameters
tStart = 0; % Start time
tEnd = 1; % End time
tStep = 0.01; % Time step size
TStart = 20; % Start temperature
TEnd = 30; % End temperature
TStep = 0.1; % Temperature step size
% Create the grid
t = tStart:tStep:tEnd;
T = TStart:TStep:TEnd;
% Initialize the solution grid
U = zeros(length(T), length(t)); % Initialize the grid with zeros
% Loop over time steps
for n = 2:length(t)
% Update the temperature values based on the PDE using finite differences
% Compute the fuzzy coefficients based on Tm and the Fourier series
alpha = a0 + a1 * t(n) + sum(ac .* cos(K*t(n)*Tm) + as .* sin(K*t(n)*Tm));
sigma = abs(sin(t(n))); % Just an example for fuzzy volatility, modify as needed
% Compute the finite difference approximation
for i = 2:length(T)-1
U(i, n) = U(i, n-1) + tStep * (dTm * (Tm - T(i)) + alpha * (Tm - T(i)) + sigma * randn);
end
end
% Inference of Risk-Neutral Probability and Optimization Problem
Fm = [10, 12, 14, 16, 18]; % Observed market prices of weather futures
tm = 1; % Expiration time of weather futures
delta = 0.95; % Risk-free discount factor
r = 0.03; % Risk-free interest rate
% Define the Risk-Neutral Expectation operator E_Q
% Modify this part based on your specific requirements
E_Q = @(I_Htm, Ft) mean(Ft(I_Htm)); % Define your E_Q function here
% Define the condition for weather futures I_Htm
% Modify this part based on your specific requirements
I_Htm = T > 25; % Define your I_Htm condition here
% Define additional variables for the E_Q calculation
% Modify this part based on your specific requirements
Ft = U(:, end); % Define your Ft variable here
% Set the optimization problem
objective = @(fQ) sum((delta * exp(-r * (tm - t)) .* E_Q(I_Htm, Ft, fQ) - Fm).^2);
x0 = [0.1, 0.2, 0.3]; % Initial guess for the risk-neutral probabilities
% Define the optimization solver options
options = optimoptions('fmincon');
options.Algorithm = 'interior-point'; % Choose the algorithm (e.g., 'interior-point', 'sqp', 'active-set')
options.MaxIterations = 100; % Maximum number of iterations
options.Display = 'iter'; % Display the iterative output
% Define constraints for fmincon
% Define the constraints for fmincon
A = [1, -1, 1; -1, 2, 3]; % Coefficients of the linear inequality constraints
b = [5; 10]; % Right-hand side values of the linear inequality constraints
Aeq = [2, 3, -1]; % Coefficients of the linear equality constraints
beq = [4]; % Right-hand side values of the linear equality constraints
lb = [0; 0; 0]; % Lower bounds on the variables
ub = [1; 1; 1]; % Upper bounds on the variables
% Solve the optimization problem
[fQ_opt, fval] = fmincon(objective, x0, A, b, Aeq, beq, LB, UB, [], options);
% Display the optimal risk-neutral probabilities and the minimum objective value
disp('Optimal Risk-Neutral Probabilities:');
disp(fQ_opt);
disp('Minimum Objective Value:');
disp(fval);
% Plot the solution
surf(t, T, U);
xlabel('Time');
ylabel('Temperature');
zlabel('Solution');

Answers (1)

Aman
Aman on 1 Aug 2023
Hi,
I understand that you are trying to solve an optimization problem and are getting the “Too many input arguments” error.
The error is coming because in the anonymous function “E_Q” definition, it is defined that it will accept two input arguments, but while calling this function, three arguments have been passed.
E_Q = @(I_Htm, Ft) mean(Ft(I_Htm)); % two inputs arguments
objective = @(fQ) sum((delta * exp(-r * (tm - t)) .* E_Q(I_Htm, Ft, fQ) - Fm).^2); % three arguments have been passed
As the expectation of the code is not clear, it is difficult to give the modified code, but in order to solve this issue, you need to modify the function definition of “E_Q” to work for two input arguments.
Please refer the following documentation to learn more about anonymous function.
I hope it helps!

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!