Hi,
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.
Thanks
σ_f^2≡∫(y-y ̂ )^2 *pdf(θ)dθ
pdf(θ)=exp⁡(-1/(2σ^2 ) *∑(y-y ̂ )^2 )
y=C1-θ1*f(x)-θ2g(x)
y ̂ =Y

7 comentarios

Raghunandan V
Raghunandan V el 12 de Mzo. de 2019
I am not able to link with your explanation and the formulas there. Could you please have a better formula. I am not to understand the formula.
ASH
ASH el 12 de Mzo. de 2019
I'm sorry but the equation was a messy one and I was trying to shorten that. I think it's better now:
matlab.jpg
Raghunandan V
Raghunandan V el 15 de Mzo. de 2019
I am still not able to understand the question. I am extremely sorry.
From my understanding. I followed your question like this:
step1: calculate y, given θ1 , θ2, f(x), g(x) and c1
step2 : find y^ from Y
step3 : find probability distribution function of θ1 , θ2,
step4 : Integrate the squares
So in what step are you facing difficulty?
ASH
ASH el 15 de Mzo. de 2019
Y is a number assigned to each x. y^ is a function of x and θ. I know the x and Y values and want to have a double integral on θ1 and θ2.
My question is only the last term of that pdf in integral which is a sum square. I don't know how to sum over a bunch of numbers in arrays and parameters (θ1, θ2) simultaneously when they are in a function handle. Should I use ordinary sum function, symsum...?
I used the sum in a loop as below but it the result is not correct:
f(x)=asinh(c(3)^2*X/(1-X/c(4)));
g(x)=X;
for i=1:length(X)
func2=@(te1,te2,X,Y)(Y-(c(1)-te1*asinh(c(3)^2*X/(1-X/c(4)))-te2*X)).^2.*exp(-(1/(2*err))*sum((Y-(c(1)-te1*asinh(c(3)^2*X/(1-X/c(4)))-te2*X)).^2));
Sigma_f2=integral2(@(te1,te2)func2(te1,te2,X(i),Y(i)),3.76E-02,4.19E-02,1.72E-04,2.25E-04)/(2*pi*err)^(n/2);
sig_f2(i)=Sigma_f2(end);
end
Thanks
Raghunandan V
Raghunandan V el 15 de Mzo. de 2019
Yup! I now understand the problem.I think you got confused with for loop(I am not sure yet) Could you please post your data matrix of all the inputs. I will try to optimize the code. I dont think for loop is required. please post a sample data of X, c1, te1, err and te2
ASH
ASH el 15 de Mzo. de 2019
Actually FOR loop was needed for another function handle that I deleted in the code that I sent for you. I used the same FOR loop for this new function handle and I'm not sure if it's still needed.
X=[0;5;10;20;30;50;100;200;300;400;500;600;700;800;1000;1200];
Y=[0.96;0.88;0.85;0.82;0.8;0.78;0.73;0.69;0.65;0.61;0.57;0.54;0.51;0.48;0.39;0.21];
c=[.961 .04 .868 1204.2 1.996e-4];
err=2.8929e-05;
te1 and te2 are c2 and c5 in the c array above, that I got them from another function. the bounds of the integral are in the integral2 function in previous comment.
The final result (sig_f2(i)) should be 16 numbers corresponding to each X and Y.
Thanks.
Raghunandan V
Raghunandan V el 15 de Mzo. de 2019
Very interesting situation! I see that the function intergral2 doesn't support any matrices. Have you tried Laplace approach?

Iniciar sesión para comentar.

 Respuesta aceptada

Raghunandan V
Raghunandan V el 15 de Mzo. de 2019

0 votos

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
X=[0;5;10;20;30;50;100;200;300;400;500;600;700;800;1000;1200];
Y=[0.96;0.88;0.85;0.82;0.8;0.78;0.73;0.69;0.65;0.61;0.57;0.54;0.51;0.48;0.39;0.21];
c=[.961 .04 .868 1204.2 1.996e-4];
%find the length of X
n = length(X);
err=2.8929e-05;
% 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!!
Sigma_f2(k)=integral2(IntegralProduct,3.76E-02,4.19E-02,1.72E-04,2.25E-04)/((2*pi*err)^(n/2));
end
%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;
end
% 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;
end
end
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,
:)

6 comentarios

ASH
ASH el 15 de Mzo. de 2019
Thanks for your reply!
You made some minor mistakes in defining functions; I corrected them and I think it worked.
I also have a problem with the similar functions except for the pdf which is a matrix this time:
matlab2.jpg
I tried both with function (like what you wrote) and without function but it cannot form the final result (in exp term) which is a scalar function of te1 and te2. and when I tried with "syms" it cannot integrate that.
I calculated the result in exp term by hand and I think it worked. but I wonder why I can't write this term as multiplication of matrices when the dimensions are correct as below:
(θ-θ ̂ )= tet=[te1-c(2);te2-c(5)];
(θ-θ ̂ )^T=tet';
Σ^(-1)=[24098846.6200120,1790369506.95800;1790369506.95800,155092386233.745];
|Σ|=1.87959347430218e-18;
Thanks.
Guillaume
Guillaume el 21 de Mzo. de 2019
I was asked by Raghunandan V in another thread to comment the code in this answer.
Note that I've not even tried to understand what is being solved. I'm only commenting and improving the code as is.
First, obviously, don't use global variables. They're just not worth the risks.
Then we have
Square = @Calculate_ydiff_square;
Sum = @Summation;
This just aliases the two functions, so instead of calling Calculate_ydiff_square we can call it Square. What's the point, why can't you call it by its original name, or rename the original function if you don't like the name.
The global variables here are just used to pass arguments to the two functions above. The easiest way to pass arguments to function is by making them actual inputs to the function. So:
function y_diff_square = Calculate_ydiff_square(te1,te2, yk, c, xk)
y = (c(1)-te1*asinh((c(3)^2*yk) / (1-xk/c(4))) - te2*xk);
y_diff_square = (y - yk).^2;
end
function SumVal = Summation(te1,te2, X, Y, c)
for k=1:numel(X) %I've not tried to understand what is happening in the loop. Most likely it's not needed
SumVal = SumVal + ((c(1)-te1*asinh((c(3)^2*X(k)) / (1-var1/c(4))) - te2*Y(k)) - Y(k)).^2;
end
end
And then to use these:
X=[0;5;10;20;30;50;100;200;300;400;500;600;700;800;1000;1200];
Y=[0.96;0.88;0.85;0.82;0.8;0.78;0.73;0.69;0.65;0.61;0.57;0.54;0.51;0.48;0.39;0.21];
c=[.961 .04 .868 1204.2 1.996e-4];
Pdf = @(te1,te2, X, Y, c) (2*exp(-(1/(2*err^2))) * Summation(te1,te2, X, Y, n, c));
for k = 1:numel(X)
IntegralProduct = @(te1, te2) Calculate_ydiff_square(te1, te2, Y(k), c) .* Pdf(te1, te2, X, Y, c);
Sigma_f2(k)=integral2(IntegralProduct,3.76E-02,4.19E-02,1.72E-04,2.25E-04)/((2*pi*err)^(n/2));
end
See Parametizing functions for how to pass extra arguments to ode, integral, etc. functions, as I've done above.
Raghunandan V
Raghunandan V el 22 de Mzo. de 2019
Editada: Raghunandan V el 22 de Mzo. de 2019
Hi,
I tried before doing like this but I get error of not enough input argument
Error in Understand1>Calculate_ydiff_square (line 23)
y = (c(1)-te1*asinh((c(3)^2*yk) / (1-xk/c(4))) - te2*xk);
Error in Understand1>@(te1,te2)Calculate_ydiff_square(te1,te2,Y(k),c).*Pdf(te1,te2,X,Y,c)
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in Understand1 (line 17)
Sigma_f2(k)=integral2(IntegralProduct,3.76E-02,4.19E-02,1.72E-04,2.25E-04)/((2*pi*err)^(n/2));
please check.
Guillaume
Guillaume el 22 de Mzo. de 2019
Indeed, I forgot the x(k) as an input
IntegralProduct = @(te1, te2) Calculate_ydiff_square(te1, te2, Y(k), c, X(k)) .* Pdf(te1, te2, X, Y, c);
% forgot ---^
Raghunandan V
Raghunandan V el 22 de Mzo. de 2019
Yup!!. Thanks a lot for the help!
ASH
ASH el 24 de Mzo. de 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?
Thanks.

Iniciar sesión para comentar.

Más respuestas (1)

ASH
ASH el 15 de Mzo. de 2019

0 votos

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)
func1=@(te1,te2,X,Y)(Y-(c(1)-te1*asinh(c(3)^2*X/(1-X/c(4)))-te2*X)).^2.*exp(-.5*(V_bi(1,1)*(te1-c(2)).^2+2*V_bi(1,2)*(te1-c(2)).*(te2-c(5))+V_bi(2,2)*(te2-c(5)).^2));
Sigma_f1=integral2(@(te1,te2)func1(te1,te2,X(i),Y(i)),3.76E-02,4.19E-02,1.72E-04,2.25E-04)/((2*pi)^n*det(V_b))^.5;
sig_f1(i)=Sigma_f1(end);
end
I have not tried other options.

3 comentarios

Raghunandan V
Raghunandan V el 18 de Mzo. de 2019
Hi,
I was out in the weekends. You cannot pass matrices into intergral2 function. As I was telling it creates its own matrix for the limits of integrals.
Coming to your code. I will try to break it down.
V_bi=[24083763.7420230, 1789248958.52452; 1789248958.52452, 154995317706.961];
I think you must be new to coding. You write the formulas correctly but you lack the skills to write it cleanly. Always add comments and do use indentation for coding in for loops. Do read a wonderful article here for good coding guidelines. Assume a third person wanting to understand the problem, he should be reading the code like a story.
for i=1:length(X)
If you see this line. By looking at the way you defined it, I would say this line need not be inside the for loop at all because you are defining it everytime inside the loop. Everytime inside the for loop it defines a new memory location which is time consuming. Only put nthe part of code which you want run multiple times.
The next point I would like to mention is the number of operations you carry out in one line. This is very difficult for people who read it. Industry standard tells that not more than 5 to 6 mathematical operations can exists in one line. break them into smaller peices and then express the equation. I really helps you to debug the errors
func1=@(te1,te2,X,Y)(Y-(c(1)-te1*asinh(c(3)^2*X/(1-X/c(4)))-te2*X)).^2.*exp(-.5*(V_bi(1,1)*(te1-c(2)).^2+2*V_bi(1,2)*(te1-c(2)).*(te2-c(5))+V_bi(2,2)*(te2-c(5)).^2));
I am not sure this works the best. You have done matrix multiplication manually!!! Hahaha. The main purpose of using the Matlab is because it can do Matrix multiplication better than what we do. So try to use matlab as much as possible!.
Sigma_f1=integral2(@(te1,te2)func1(te1,te2,X(i),Y(i)),3.76E-02,4.19E-02,1.72E-04,2.25E-04)/((2*pi)^n*det(V_b))^.5;
Could you please explain what this last line does. I dont feel any use for it!
sig_f1(i)=Sigma_f1(end);
end
ASH
ASH el 18 de Mzo. de 2019
Thanks for your reply,
The point is that the last comment I made in this conversation was on 15 Mar 2019 at 22:54 which I just explained the situation. The next one is the repetition of an older comment that I already sent for explaining my first question. I don't know why it was repeated!
I wrote the last line to have the result in one vector. I made a mistake in that code and the Sigma_f1 size was increasing in each step and I just wanted to have the last value of each step.
So, if the integral2 cannot handle matrices, how should I introduce the new pdf to it?
by the way, yes I'm new on coding because my major is mostly experimental but I want to learn it. I appreciate it if you can introduce any resources so that I can build my Matlab skill.
Thanks
Raghunandan V
Raghunandan V el 25 de Mzo. de 2019
Editada: Raghunandan V el 25 de Mzo. de 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 :
doc
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

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

ASH
el 12 de Mzo. de 2019

Editada:

el 25 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by