Info

This question is closed. Reopen it to edit or answer.

Loss_Fun how to programm it

1 view (last 30 days)
Farah Shahpoor
Farah Shahpoor on 21 May 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
Dear Community,
I have an optimazation problem.
I want to minimize the function:
L = Exp(0.5*pi)+ Exp(0.25*x)
I have the following code and need to understand it.
function [ LossVal, vol_pi, vol_x,vol_s ] = Loss_Fun( data, w_pi, w_x )
vol_pi = (mean(data.pi.^2));
vol_x = (mean(data.x.^2));
vol_s = (mean(data.st.^2));
LossVal = w_pi * vol_pi ...
+ w_x * vol_x ...
+ w_s * vol_s;
w_pi = 0.5 ; %1;
w_x = 0.25; %0.5;
end
As I understand w_pi and w_x are the weights of the paramater pi and x.
My problem is what is meant by data: Loss_Fun( data, w_pi, w_x )
vol_pi vol_x and vol_s should the mean, which I need for the function.
When Iam using this code for my model matlab gives me the errror:
Not enough input arguments.
Error in Loss_Fun (line 3)
vol_pi = (mean(data.pi.^2));
From where does matlab take this
vol_pi = (mean(data.pi.^2));
vol_x = (mean(data.x.^2));
vol_s = (mean(data.st.^2));

Answers (1)

Saumya Goel
Saumya Goel on 24 May 2019
Whenever Loss_Fun is called, there are 3 input arguments that needs to be passed. So, data is one of them, which should contain -pi,x and st values on basis of which it will calculate the variables vol_pi,vol_x,vol_s.
Also, if you are looking for predefined functionalities on Optimization Problem, you may consider having a look at Optimization Toolbox: https://www.mathworks.com/help/optim/write-objective-function.html
  1 Comment
Farah Shahpoor
Farah Shahpoor on 24 May 2019
Thanks for your reply.
This is my code now. When I run the code I am getting the error:
Not enough input arguments.
Error in lossc (line 3)
vol_pi = (mean(data.v_solution(2).^2));
and so on. What is the problem here?
function [ LossVal, vol_pi, vol_x ] = Loss_Fun( data, w_pi, w_x )
vol_pi = (mean(data.v_solution(2).^2));
vol_x = (mean(data.v_solution(1).^2));
LossVal = w_pi * vol_pi ...
+ w_x * vol_x;
w_pi = 0.5 ; %1;
w_x = 0.25; %0.5;
end

Tags

Community Treasure Hunt

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

Start Hunting!