How to Sum functions?

80 views (last 30 days)
ASH on 12 Mar 2019
Edited: Raghunandan V on 25 Mar 2019
I have an ugly function which I want to integrate it with respect to θ1 and θ2 in a function handle. X and Y are two n*1 arrays. I wonder how can I do the summation so that at the end (before integration) I have the result as sum square of n functions in terms θ1 and θ2? That is, this sum should take values of X and Y from the arrays simultanouesly and plug into y and y^ respectively.
σ_f^2≡∫(y-y ̂ )^2 *pdf(θ)dθ
pdf(θ)=exp⁡(-1/(2σ^2 ) *∑(y-y ̂ )^2 )
y ̂ =Y
Raghunandan V
Raghunandan V on 15 Mar 2019
Very interesting situation! I see that the function intergral2 doesn't support any matrices. Have you tried Laplace approach?

Sign in to comment.

Accepted Answer

Raghunandan V
Raghunandan V on 15 Mar 2019
Uff!!!. I think I finally found a solution to this. I will not assure this is the best way to do it.
clear all;
%initialize global variables
global X;
global Y;
global n;
global var2; %this provide Y(k)
global var1; %this provides X(k)
global c;
% get the inputs
c=[.961 .04 .868 1204.2 1.996e-4];
%find the length of X
n = length(X);
% define Sigma_f2
Sigma_f2 = zeros(n,1);
This above part is only for initialization. The next part is used for calculation. I have defined the function names as per their function. Previously defined global variables are used in the functions below.
% this creates a function handle for (Y-y^)^2
Square = @Calculate_ydiff_square;
% this is the part where you got stuck. I created a different handle for this purpose. you can look at the working below
Sum = @Summation;
%similar function handle to calculate pdf
Pdf = @(te1,te2)(2*exp(-(1/(2*err^2)))* Sum(te1,te2));
%here we multiply and two functioan handles and prepare it for integration!
IntegralProduct = @(te1,te2)(Square(te1,te2).*Pdf(te1,te2));
for k =1:n
%get the value of X and Y required for calculation
var1 = X(k);
var2 = Y(k);
%Intergrate it!!
%calculate the (y-y^)^2
function y_diff_square = Calculate_ydiff_square(te1,te2)
% define the glovbbal variable used here
global var2;
global c;
global var1;
y = (c(1)-te1*asinh((c(3)^2*var1) / (1-var1/c(4))) - te2*var1);
% return the required value
y_diff_square = (y - var2).^2;
% the Mystery box of this function!:)
function SumVal = Summation(te1,te2)
SumVal = 0;
global X;
global Y;
global n;
global c;
% re-iterate the values for all values of x and Y and then Add it
for k=1:n
var1 = X(k);
var2 = Y(k);
%Here function cannot be called as the var1 and var2 is different for this case and hence cannot be made global
SumVal = SumVal + ((c(1)-te1*asinh((c(3)^2*var1) / (1-var1/c(4))) - te2*var1) - var2).^2;
I am still not sure with the solution. please let me know if this works fine
The reason why matrix cannot be used in integration is during integration Matlab makes its own matrix for all the values in the range of the integrals!. So having a Input matrix of different size cannot be processed,
ASH on 24 Mar 2019
Thanks for your help but I didn't understand what happened to my question that I asked on 15 Mar 2019 at 22:54. The new pdf contains matrices and there is no summation in there. Is there any way to put this new pdf into integral2 without having interference on matrices?

Sign in to comment.

More Answers (1)

ASH on 15 Mar 2019
Actually I used integral2 for another function as below and apparantly it worked (it doesn't have the sigma in it) but I'm not sure about the answer:
V_bi=[24083763.7420230, 1789248958.52452; 1789248958.52452, 154995317706.961];
for i=1:length(X)
I have not tried other options.
Raghunandan V
Raghunandan V on 25 Mar 2019
Edited: Raghunandan V on 25 Mar 2019
I would say the best resource for learning matlab is the Matlab documentation itself
Just read the doc for matlab. This can be obtained by typing :
Please do check the code by Guillaume for the previous question. i asked their help as my code had global variables.
Its often a bad practice to have global variables!
There is a course on coursera which teaches machine learning. I wouldn't recommend to take the complete course but a part of it where he teaches Octave which is very similar to Matlab
Go through this. It will help you a lot

Sign in to comment.


Find more on Loops and Conditional Statements 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!