Trouble with using the function 'solve' to obtain decision boundary

I need to find the point where the decision boundary occurs for a single feature of the iris data set. I am certain i need to solve the equation P(x/SWL) = P(x/VWL) and obtain the solutions for x.I get an error for the code i tried below. The error states x is an undefined variable. Can you tell me where i am going wrong?
load fisheriris
SWL=meas(1:50,1)
VWL=meas(51:100,1)
V1 = var(SWL);
V2 = var(VWL);
M1 = mean(SWL);
M2 = mean(VWL);
P1 = (1/(sqrt(2*pi*V1))*(exp(((x-M1)^2)/(2*V1))));
P2 = (1/(sqrt(2*pi*V2))*(exp(((x-M2)^2)/(2*V2))));
solve(P1-P2);

 채택된 답변

Star Strider
Star Strider 2015년 2월 3일
The easiest way is to use the fzero function:
P1 = @(x) (1/(sqrt(2*pi*V1))*(exp(((x-M1)^2)/(2*V1)))); % ‘P1’ As Anonymous Function
P2 = @(x) (1/(sqrt(2*pi*V2))*(exp(((x-M2)^2)/(2*V2)))); % ‘P2’ As Anonymous Function
X = fzero(@(x) P1(x)-P2(x), 1) % Create Anonymous Funciton, Solve For ‘x’
produces:
X =
3.0804

댓글 수: 6

Thank you. I just learnt something new.
My pleasure!
That occurs frequently for all of us here on MATLAB Answers!
Is there an another way of solving this equation since fzero gives me the correct answer only if i know that the solution i need lies closer to 5 instead of 1. Is there a way to obtain multiple solutions?
The best way to obtain multiple solutions from fzero is to iterate in a for loop, with a different initial guess at each iteration.
For example:
IV = linspace(0, 10, 20); % Vector Of Initial Guesses
for k1 = 1:length(IV)
X(k1) = fzero(@(x) P1(x)-P2(x), IV(k1)); % Create Anonymous Function, Solve For ‘x’
end
Xu = unique(fix(X*1E+4)/1E+4); % Find Unique Elements Of ‘X’
produces:
Xu =
3.0803 5.3062
It is necessary to use a ‘roundoff’ approximation with the unique function because the inherent approximation errors in floating-point calculations do not guarantee that every solution near 3 or 5 will be exactly the same to full floating point precision.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2015년 2월 3일

댓글:

2015년 2월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by