Info
This question is closed. Reopen it to edit or answer.
Help regarding usage of Anonymous functions
1 view (last 30 days)
Show older comments
Hi, I am newbie to matlab and require some help in the usuage of Anonymous functions for the following scenario. I would like to implement the below equation using anonymous function.
The above probability should be function of Deltax
the conditional probability is given by
m and n are constants
and the probability of A is given by
In all the above equations, execpt Delatax every other is constants.
Could someone give some suggestions how to approach this scenario using anonymous functions
2 Comments
Star Strider
on 27 Feb 2016
For starters, define ‘conditional probability’ and ‘A’ in this context.
The three expressions all simply appear to be floating in space with no obvious relationship to each other.
Answers (2)
Konstantinos Sofos
on 27 Feb 2016
Edited: Konstantinos Sofos
on 27 Feb 2016
Hi,
Firstly i fully agree with the comment of Star Strider. Because as you say you are "newbie" to matlab i would suggest you to read what is an Anonymous Function. If you read this, then it will be very easy to understand that you could write simple e.g your second ?equation? as
my_eq = @(dx)(.5*(erf(m*dx/2*sigma_n)-erf(n*dx/2*sigma_n));
% And run a test case
a = my_eq(0.5)
After this i hope you become expert ;)
1 Comment
Star Strider
on 28 Feb 2016
This is how I would do it:
P_qi_a = @(l,i,dx,sh) (erf((l(1) + i.*dx)./(sqrt(2)*sh)) - erf((l + (i+1).*dx)./(sqrt(2)*sh)))/2;
P_qj_qi = @(m,n,dx,sn) (erf(m.*dx./(2*sn)) - erf(n.*dx./(2*sn)))/2;
dx = ...; % Define Constants
sh = ...;
m = ...;
n = ...;
sn = ...;
N = ...;
i = 1:N; % Define Vector
l = ...; % Define Vector
P_qj = @(dx) sum( P_qj_qi(m,n,dx,sn) .* P_qi_a(l,i,dx,sh) );
[P_qj_1, fval] = fzero(@(dx) P_qj(dx)-1, 0.5); % Solve For ‘P_qj = 1’
This is based on what you’ve posted thus far, so if there are any errors in them, you will need to correct them. If ‘l’ is a function of ‘i’, you will need to create a vector for it that is equal in length to ‘i’
NOTE — This is obviously UNTESTED CODE so I cannot guarantee that it will produce the results you want, or for that matter, that it runs correctly. That’s the problem with hypothetical problems such as this.
I have no idea what you’re doing, so you’re on your own from here.
2 Comments
Star Strider
on 28 Feb 2016
My pleasure.
I cannot follow what you are doing. All I did was create anonymous functions for your equations, as you requested, just as you posted them. You want to find the value of ‘dx’ (delta x) where the summation equals 1, and the fzero call does that (providing that zero-crossing exists, since the function itself appears continuous and continuously differentiable). All my equations are vectorised, so they should be robust. I will leave you to apply them to your data. You are free to change them and adapt them as you wish.
If you have vectors or matrices you need to multiply or do other operations on, see the documentation on Array vs. Matrix Operations. Another useful function for what you want to do is bsxfun. Experiment with bsxfun on sample data that do not involve the anonymous functions I wrote for you so you can learn how it works. If you are doing fundamental matrix and array operations and calculations, see the documentation covering those areas.
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!