How to Sum functions?

61 次查看(过去 30 天)
ASH
ASH 2019-3-12
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 个评论
ASH
ASH 2019-3-15
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 2019-3-15
Very interesting situation! I see that the function intergral2 doesn't support any matrices. Have you tried Laplace approach?

请先登录,再进行评论。

采纳的回答

Raghunandan V
Raghunandan V 2019-3-15
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 个评论
Raghunandan V
Raghunandan V 2019-3-22
Yup!!. Thanks a lot for the help!
ASH
ASH 2019-3-24
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.

请先登录,再进行评论。

更多回答(1 个)

ASH
ASH 2019-3-15
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 个评论
ASH
ASH 2019-3-18
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 2019-3-25
编辑:Raghunandan V 2019-3-25
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by