Re: help implementing membership function in C...

WSiler (wsiler@aol.com)
Fri, 18 Dec 1998 22:34:15 +0100 (MET)

>I'm attempting to solve a control problem with fuzzy instead
>of a PID based scheme. I understand most of what I've
>read but I'm having some problems writing the code.

>The target will be an HC12 microcontroller.

>For example if my membership function looks like:

> 1|\
> | \
> | \
> | \
> | \
> | \
> | \
> 0--------5

>I can't figure out the math to determine memership for an x
>value less than 5. I know that I can't use floats since the
>HC12 does not handle float values.

>From the book I've been reading there is example source
>code for the function:

>if (speed >=0 && speed <=0)
> result=100;
>else if (speed >=0 && speed <=5)
> result=100 - (((speed - (0))*200)/10);

>In the book it seems like the '100-' part is used for
>membership functions with a negative slope. and the
>'speed-(0)' is the speed minus the lower bound of the
>function. I just can't figure out where the '200' came from.

>Any help, or pointers to help would be greatly >appreciated

To begin with, there is no need to use floating point; take your membership
values from zero to say 1000. Similarly, just multiply your x values by say
100, so you can work in integer arithmetic. If you need more resolution,
increase these values as much as you wish within the limits of your word size
on your microprocessor, watching out for overflows and underflows. Say you have
a 16-bit word available; you don't want any intermediate calculations to go
bigger than 32k if you want to be able to handle negative numbers. Let's say
your function given above is the membership function for Small. Then, for your
graph, the membership value of x would be, mathematically:

S(x) = 1000 * ((500 - x) / 500)

but this will give you an underflow on the ((500-x)/500) part. So we rearrange
the calculation like this:

S(x) = (1000 / 500) * (500 - x), 0 <= x <= 500 or
S(x) = 2 * (500 - x), , 0 <= x <= 500
S(x) = 0, x > 500
S(x) = 1, x < 0

which will work OK. Similarly for the other membership functions going up from
yours. For Medium, we might have

M(x) = 0, x < 0
M(x) = 2 * ( x), 0 <= x <= 500
M(x) = 2 * (1000 - x), 500 < x < 1000
M(x) = 0, x >= 1000

and so on. Something like this was probably going on in your book example, but
I'm too busy to figure it out. There's at least one bad error in the code you
give. (speed >=0 && speed <=0) is only true for x == 0.

William Siler

############################################################################
This message was posted through the fuzzy mailing list.
(1) To subscribe to this mailing list, send a message body of
"SUB FUZZY-MAIL myFirstName mySurname" to listproc@dbai.tuwien.ac.at
(2) To unsubscribe from this mailing list, send a message body of
"UNSUB FUZZY-MAIL" or "UNSUB FUZZY-MAIL yoursubscription@email.address.com"
to listproc@dbai.tuwien.ac.at
(3) To reach the human who maintains the list, send mail to
fuzzy-owner@dbai.tuwien.ac.at
(4) WWW access and other information on Fuzzy Sets and Logic see
http://www.dbai.tuwien.ac.at/ftp/mlowner/fuzzy-mail.info
(5) WWW archive: http://www.dbai.tuwien.ac.at/marchives/fuzzy-mail/index.html