How to Sum functions?
32 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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 个评论
更多回答(1 个)
ASH
2019-3-15
3 个评论
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
the link for the course is https://www.youtube.com/watch?v=39PyhM0LAow&list=PLLssT5z_DsK-h9vYZkQkYNWcItqhlRJLN&index=25
Go through this. It will help you a lot
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!