Info

此问题已关闭。 请重新打开它进行编辑或回答。

Help regarding usage of Anonymous functions

1 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2016-2-27
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.
SRIHARSHA KORADA
SRIHARSHA KORADA 2016-2-28
Sorry for not asking the question clearly.
First Equation:
Second Equation
Third Equation:
Now the thing is I am trying to calculate the dx for which the first equation is 1.As I mentioned, except dx, all are constants. When I am trying to generate the second equation in the form of an array, I am unable to get the functions for different values of i. Could u help me please

回答(2 个)

Konstantinos Sofos
Konstantinos Sofos 2016-2-27
编辑:Konstantinos Sofos 2016-2-27
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 个评论
SRIHARSHA KORADA
SRIHARSHA KORADA 2016-2-28
I am giving the equations again
First Equation:
Second Equation
Third Equation:
Now the thing is I am trying to calculate the dx for which the first equation is 1. So I am also using fzero finally after trying to multiply the 2 and 3 equations in the form of an array. But, it gives an error saying function handles cannot be multiplied.As I mentioned, except dx, all are constants. When I am trying to generate the second equation in the form of an array, I am unable to get the functions for different values of i.All are hardcoded with just the letter 'i' itself. I hope, i have asked the question clearly. Could u help me please

Star Strider
Star Strider 2016-2-28
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 个评论
SRIHARSHA KORADA
SRIHARSHA KORADA 2016-2-28
Thank you for ur answer. I have some doubts. In the above conditional probability definition, m=j-i+0.5 and n = j-i-0.5, and j and i runs from 1 to n. Defining the vectors as u mentioned, is not calculating for every value of j given i value. It is just doing normal array subtraction. And direct multiplication as u mentioned is not what I want. Assuming conditional probability as matrix for every value if i and j and marginal probability of A as a array for i, then I need to multiply every column of Conditional probability matrix with the array of marginal probability.
Could you tell what is the way for this
Star Strider
Star Strider 2016-2-28
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.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by