Introduction to Fuzzy Logic with MATLAB and Simulink - MATLAB
Video Player is loading.
Current Time 0:00
Duration 35:14
Loaded: 0.00%
Stream Type LIVE
Remaining Time 35:14
 
1x
  • descriptions off, selected
  • en (Main), selected
    Video length is 35:14

    Introduction to Fuzzy Logic with MATLAB and Simulink

    Overview

    In this webinar you will learn the benefits of using fuzzy logic, its application areas, and, how to design fuzzy inference systems using Fuzzy Logic Toolbox.

    This webinar will cover the newer capabilities of Fuzzy Logic Toolbox, such as redesigned Fuzzy Logic Designer app and support for type-2 fuzzy systems. You will see how to use the Fuzzy Logic Designer app to design a fuzzy inference system, and then integrate the designed fuzzy system into Simulink for system-level simulation and code generation.  

    You will additionally learn how to use fuzzy logic as one of the techniques to explain the behavior of AI-based black-box models using its explainable rule base.

    Highlights

    • Introducing fuzzy logic and its application areas
    • Designing, analyzing, and evaluating fuzzy inference systems  using the Fuzzy Logic Designer app
    • Integrating the designed fuzzy system into MATLAB and Simulink for system-level simulation and code generation
    • Using fuzzy logic to explain the behavior of AI-based black-box models

    About the Presenter

    Kishen Mahadevan is a Product Manager for Fuzzy Logic Toolbox, System Identification Toolbox, and Simulink Design Optimization at MathWorks. Kishen has an M.S. in Electrical Engineering with specialization in Control Systems from Arizona State University, and a B.E. in Electrical and Electronics Engineering from Visvesvaraya Technological University in India. He joined MathWorks in 2018 as an Application Support Engineer helping customers resolve issues related to Simulink products before moving into product marketing.

    Recorded: 14 Dec 2022

    Hello, everyone. Welcome to the session on Introduction to Fuzzy Logic with MATLAB and Simulink. My name is Kishen Mahadevan. I'm a Product Manager at MathWorks focusing on the area of fuzzy logic. I've been with MathWorks for a little over four and a half years now and I work closely with the development team to help guide the roadmap of our control tools. I also have a background in electrical engineering with specialization in control systems.

    Before I start this webinar, I want to give you a quick glimpse of what you can expect to see over the course of this session. In this webinar, you will learn about fuzzy logic systems in general and you'll see how you can create, analyze, and evaluate these systems using capabilities from Fuzzy Logic Toolbox and specifically using the Fuzzy Logic Designer app. You'll then see how you can export the created fuzzy system and bring it into either a MATLAB or Simulink model for system level simulation and code generation workflows.

    As I highlight the capabilities of the product and go through the demonstration, the goal of the session is for you to walk away with these points. That is you can use fuzzy logic to develop complex controls and decision making systems that can be described by rules. You can create fuzzy systems without having to be an expert in fuzzy logic using the Fuzzy Logic Designer app. And finally, you can integrate your designed fuzzy inference systems with the larger MATLAB or Simulink model for system level simulation, testing of different scenarios and simulation platform, and finally for code generation workflows.

    So before we jump into the demonstration and show you how it works, I think many of you here might benefit from a quick overview of fuzzy logic. So we'll take a step back and see what is fuzzy logic, why use it, and what are the potential areas where fuzzy logic can be used.

    So let's start with what is fuzzy logic. So let's consider this exercise. If I were to ask you how your day has been so far, some of you here might say it has been pretty good, some might say not great, and some might even say it's just been OK. So now let's focus on the answer of pretty good. So what is pretty good to you might not be the same as when I say pretty good or when somebody else says pretty good. That is the definition of pretty good can vary from person to person.

    So unlike a binary logic where there is just one truth to the statement, unlike being just true or false or just 0 or 1, fuzzy logic is a degree of truth. And it contains a range of values between 0 and 1. So that is fuzziness and that is vagueness. And I will let you hold on to this thought for a minute.

    Now let's consider another example where you have to tip a waiter at a restaurant based on the quality of food and the quality of service that you've received the last time you visited the restaurant. And the way you would go about figuring out the tip percentage would be that it would be based on some logical rules such as this. Something like if the service that you experienced was excellent and the food that you had was delicious, then you would perhaps tip generously. Or on the other hand, if the service that you received was poor or the food that you had was not great or rancid, then the tip percentage would be low.

    So the decisions that you make here are based on logical rules. So fuzzy systems try to solve the problem in a similar way. That is fuzzy systems mimic the way humans try to solve problems by mapping inputs to outputs using logical rules, which are easily interpretable.

    Now, in terms of what is the benefit of using fuzzy logic or why use fuzzy logic. So fuzzy logic can be extremely beneficial and can be used to model complex decision systems that are difficult to describe using other modeling approaches generally but can be described by rules.

    To demonstrate this, let's take the same example of the tipping problem where you want to create a mapping of inputs to outputs, something like the image that you see on the screen. So if you can focus on the image, you'll see that you want to provide a generous tip when the quality of service and the quality of food is high. And you want to provide a lower tip when the quality of food and the quality of service is bad. And the flat area in between shows the average tip percentage at about 15%, which is the average tip percentage in the US.

    So let's try to do this using the non fuzzy approach first and then we'll come to the fuzzy approach. So with the non fuzzy approach, this is what the MATLAB code would look like if you were to try it. Right now this code is parameterized so that you can change the definition of good, bad food and service, and cheap and generous tip in numerical terms.

    And this code creates this piecewise linear surface, the mapping surface that we saw in the image. So as you can see even for this simple initial model that we started off with, the code is already looking cluttered and is difficult to understand and probably difficult to maintain.

    Now say if I want to make modifications to the surface, say I want to make this tipping system more complex or better by making a few changes in the surface, to make such modifications and translate that into MATLAB code with a non fuzzy approach could be challenging to incorporate it.

    On the other hand, with the fuzzy approach, you can formulate the same answer using simple linguistic rules, similar to how humans tried to solve this problem. Here even if you want to make modifications to the surface, that is by say you want to make it smoother or make certain areas of the surface sharper to make the system better, you can do that by just modifying the rules or perhaps just by making few changes to the range of the values for each of those fuzzy parameters or fuzzy variables. This way it is easy to modify, maintain, and understand fuzzy systems.

    So this is where fuzzy logic adds value. That is by making it easier to create complex systems for which you might not know much about the underlying mathematical equations or dynamics, but you have some expert knowledge which you can encode in the form of logical rules.

    And fuzzy logic can be used for a variety of problems and applications. It can be used for control design applications across medical applications, autonomous driving, robotics, et cetera. So let's consider an example of this. An example of fuzzy logic being used for control system design can be this, where this is an insulin dosage controller where the controller should decide the amount of insulin that should be injected into the diabetic patient.

    Now, this is a complex system to model. But if you were to talk to a doctor or a clinician or anybody who's an expert in this field, they would definitely have some knowledge in terms of how much of an insulin dose should be injected into a patient at different time. You can then use this knowledge and encode it in the form of logical rules to create this fuzzy system or to create this controller.

    Similarly, this is true for applications involving any nonlinear controller that can use expert knowledge in the form of rules. And fuzzy logic on the other hand can also be used for intelligent decision making or for creating intelligent decision making systems for finance related applications, for scheduling, for resource allocation, and so on.

    An example for this could be this scenario. So consider that you are going to a bank to apply for a loan. So now consider how would this banker in the image go about deciding if he should grant a loan to you or not? The banker would perhaps consider a lot of factors, such as your income, your credit history, your credit score, your collaterals, your history of loans, and so on. So this is a complex decision making system to model because it takes in a lot of factors into account.

    And this is another scenario where the banker's knowledge can come into play to implement the system with rules using fuzzy logic. And again, this is true for applications involving our decision making system that can use expert knowledge in the form of rules. Similarly, fuzzy logic can also be applied for signal processing applications for image processing, speech processing, image segmentation, and so on.

    Now that we know what is fuzzy logic, why use it and its applications? Let's now look at how to create fuzzy inference systems. To help you create fuzzy systems, we have a dedicated toolbox called Fuzzy Logic Toolbox. The toolbox provides capabilities through which you can use the Fuzzy Logic Designer app to perform interactive workflows through a graphical user interface to design and evaluate fuzzy systems. You can use fuzzy tuning capabilities to learn rules and membership function parameters from data.

    You can also leverage functions that come with the toolbox to design and evaluate type one and type two Mamdani and Sugeno fuzzy inference systems. You can bring the estimated models into Simulink using built in blocks to perform integrated workflows and for system level simulation. You can deploy your designed fuzzy system with our various coder tools by generating C, C++, or structured text code. Finally, you can use the reference examples provided with the toolbox to quickly get started.

    And using the toolbox, the common workflow that you can use to create and evaluate fuzzy system is you can start by defining inputs and outputs to your system. You can then define membership functions. These are nothing but setting ranges to the values of your fuzzy variables similar to pretty good, OK, and not great. After you've defined the membership functions, you can then create rules to map these inputs to outputs. And then finally, evaluate your fuzzy system.

    Now, all of these steps mentioned above can be accomplished either by using command line functions provided with the toolbox or using the Fuzzy Logic Designer app. However, it is often more convenient to use the graphical user interface, and that is what we will do here. So let's take a look at an example where I show how to create fuzzy system using the Fuzzy Logic Designer app.

    So in this example, we'll see how you can create an insulin dosage controller using fuzzy logic. This is one of the examples that I briefly mentioned earlier. In this system when a diabetic patient eats, their blood glucose level shoots up and there is a need for a controller to maintain the glucose level by regulating the insulin doses. Here we'll use fuzzy logic as a controller to regulate the insulin dosage.

    We'll start by creating a fuzzy logic controller using the Fuzzy Logic Designer app. Will then import the designed fuzzy system into Simulink to test out different scenarios of different meal intake values and to perform system level simulation. And we'll finally show you how you can generate code for deployment by generating code from the fuzzy logic controller block or for your entire Simulink model through the Simulink coder.

    So let's go ahead and take a look at the demo. To open the Fuzzy Logic Designer app, you can type the Fuzzy Logic Designer command in the Command window. This opens up a start page that provides a standard template fuzzy systems to get started, to quickly get started with your design, such as Mamdani and Sugeno type one and type two systems. So for our example, let's start with the Mamdani type one template.

    On loading this template, you see that the Fuzzy Logic Designer app opens up with the standard fuzzy system with two inputs and one output. To design our system, which is the insulin dosage controller, the first step is to define the number of inputs and the outputs. For our system, our system also takes in two inputs and gives out one output. The two inputs that we take are the blood glucose level and the blood glucose rate. And the output of the fuzzy system is the insulin dosage.

    So let's go ahead and define these inputs and outputs. So the first input is the blood glucose level. The second input is the blood glucose rate. And the output is our insulin dosage.

    So now that we've defined our inputs and outputs, the next step is to define the membership functions for each of these inputs and outputs. So let's start by defining the membership functions for the blood glucose level. Again, just to recall, the membership functions are nothing but setting ranges to your fuzzy variables for the different inputs and the outputs.

    So for the first input, which is the blood glucose level, I'm going to set the range to be between 80 to 120 where 80 represents the normal blood glucose level and 120 represents very high blood glucose level. And I'm going to define three membership functions. That is three fuzzy variables. I'm going to define normal as one of them. The second one is going to be high. And I'm going to define the third one as very high.

    And here to represent the range of these values, you can use a variety of distributions that are available with the Fuzzy Logic Toolbox. In this case, I'm going to use a triangular membership function. And I'm going to set the parameters for each of these fuzzy variables. Again, you can set up these variables based on your intuition of what you feel is the right set of values for the type of membership functions that you are defining.

    And you can see the defined membership functions graphically using the Membership Function Editor. So in the editor, you see that I've defined the normal blood glucose level to be at about 80 and it drops down drastically, reduces as it goes towards 95. And I've defined the high value of blood glucose levels peaking at about 100 and I've defined the very high blood glucose level peaking at about 120.

    I also want to point out here is that you can make changes to these membership function parameters graphically by moving around these points in the Membership Function Editor. This way you can go about defining the membership functions for the other input, which is the blood glucose rate as well as the output, which is the insulin dosage.

    In the interest of time, I'm not going to be doing it live, but I have already loaded a version where I've already defined these membership functions. And I can quickly show you how that looks. So for the blood glucose rate, I have defined three membership functions with negative, 0, and positive. And for the insulin dosage, which is our output, I've defined five levels ranging from very low insulin dosage to very high in the range of 0 to 2, 0 being very low, which is no insulin dosage at all, and very high being the highest value, which represents an insulin dosage of two units.

    Now that we've defined the membership functions for all of our inputs and outputs, the next step is to define these rules, to map these inputs to outputs. You can go about implementing these rules by including the intuition you have about your system. For example, you can create a rule such as this. You can say that if the blood glucose level is normal and your blood glucose rate is 0, that is under normal conditions, then perhaps the insulin dosage should be low to very low, because the blood glucose level is already under a normal condition.

    On the other hand, another rule could be something like if your blood glucose level is, say, very high and your blood glucose rate is positive, which is indicative that your blood glucose level is still on the increasing trajectory, then the insulin dosage that we should inject into the patient should perhaps be the highest available, because the blood glucose level is high and the rate is positive.

    So this way you can go about creating rules by including the intuition and the expert knowledge you have about your system to map these inputs and outputs. And again, you can implement these rules for all possible combinations of the input membership functions. I'm going to load a version where I've already created these rules. And this is what it looks like.

    Now that we've created a fuzzy system using these rules, the next step is to evaluate our fuzzy system. To evaluate our fuzzy system, you can open up the Rule Inference tab. Using the Rule Inference tab, you can use these sliders to model different scenarios of input values. That is you can set different values of blood glucose level and different values of blood glucose rate and see what the corresponding insulin dosage is. So this way you can verify the performance of your fuzzy system and validate that it is working as expected.

    Another approach or another way to verify this performance is by looking at the control surface. Where in the control surface the z-axis is the insulin dosage, the x-axis is the blood glucose level, and the y-axis is the blood glucose rate. And this control surface shows the overall mapping of all the inputs and the output, which is the insulin dosage.

    On quickly looking at this control surface, one thing that is already standing out to me is this region of this control surface. That is when the blood glucose level is at about 100, which is just about the normal range, and when the blood glucose rate is negative, our current fuzzy system is injecting a very high dosage. This is not something as per my expectations.

    You can also verify this behavior by taking a look at the rule inference step. I'm going to reorganize the tab so that we have a better view of all these tabs. All right. And I'm going to load the control surface here.

    To verify this behavior in the Rule Inference tab, I'm going to change the slider and set the blood glucose level to somewhere close to 100 and I'm going to set the blood glucose rate to be negative. And as you can see, the insulin dosage is 1.9 units, which is very high for this scenario. And this is not as what I expected.

    And one way to go back and change this is by quickly looking at the row in the rule inference that highlights or that contributes to most to this insulin dosage. In this, the rule inference says it's the second row, which means that it would correspond to the second rule in your fuzzy system, which is that if the blood glucose level is high and blood glucose rate is negative, then insulin dosage is very high.

    I think this must have been an error in terms of setting this. So I can quickly change this rule to say that if my blood glucose level is high and the blood glucose level is negative, which means that it's already coming back to the normal range, then I don't need an insulin dosage to be very high but rather a low dosage of insulin.

    On changing this value to low, you can see that the control surface and the rule inference have been updated. Now the control surface looks more like what I expect my fuzzy system to behave. And on quickly verifying this in the rule inference, you can see that for a blood glucose level of 100 and the blood glucose rate being negative, the insulin dosage is 0.5 units, which is as per my expectation.

    So this way you can quickly validate and verify that your fuzzy system is behaving the way you expected by using the control surface as well as the rule inference. So now we have designed and evaluated an insulin dosage controller using fuzzy logic.

    I also want to point out here is that you can design other types of fuzzy inference systems using this Fuzzy Logic Designer app and you can quickly convert the existing system into different types by using these options. That is by converting it into a Sugeno system or into a type two system as well.

    A few reasons why you might want to use Sugeno system is that the defuzzification process in a system is slightly different. That is the way the insulin dosage values are calculated based on the rules and based on the input membership functions are slightly different. And that can sometimes be more computationally efficient than the Mamdani system. So depending on your application and depending on your need, you can design these different fuzzy systems.

    On the other hand, you can also create these type two systems where if you want to handle more uncertainties in your fuzzy system or in your controller or if you need more smoother transitions between different regions in your control surface, then type two systems could be a better fit for your application.

    For our example of interest and for the exercise today, we'll stick with the created Mamdani system and we'll export that to the MATLAB workspace. The exported fuzzy system can now be integrated into the larger MATLAB or Simulink model for system level simulation and code generation workflows. In this example, we'll show you the Simulink workflow.

    I've already created a Simulink model where I've modelled a diabetic patient, where I've modelled a glucose monitoring system, which basically measures the blood glucose level of the diabetic patient and passes that on to the controller. The controller in this case is the fuzzy logic controller, and I've brought the designed fuzzy logic controller into the Simulink model using this built in fuzzy logic controller block that loads the designed fuzzy system from the MATLAB workspace. And this fuzzy controller regulates the insulin dosage into the diabetic patient. I've also added this meal subsystem to simulate different scenarios based on the time of the day a meal has been had and the carbohydrate intake.

    Let's first go ahead and run the system in an open loop. That is without controller in place. In this plot, the first subplot shows the blood glucose level of the diabetic patient. The second subplot shows the insulin dosage. And the third subplot shows the amount of carbohydrate intake at different time of the day. And in the open loop setting, we can see that the blood glucose level goes to very high values when there is no corrective insulin dosages being injected into the patient.

    Now let's go ahead and run the simulation in a closed loop setup. On running the simulation and seeing the results, we see that the fuzzy logic controller is able to inject corrective insulin dosages based on the measured blood glucose level of the patient, and it's able to maintain the blood glucose level at the normal range.

    Additionally, you can simulate different scenarios by updating this meal subsystem. In this case, I'm going to change the amount of carbohydrate intake by almost doubling it to see if the fuzzy logic controller is still able to regulate the insulin. And on updating this and simulating the model, we can see that the fuzzy logic controller is still able to regulate the corrective insulin dosages and maintain the blood glucose level of the diabetic patient at a normal level.

    Now that we've validated the fuzzy logic controller in the system level model in Simulink across different scenarios and that we're happy with the fuzzy system, we can go ahead and generate code for the fuzzy logic controller. You can either generate C, C++ code or PLC code for the physiologic controller. In this case, I'll go ahead and generate C code for this controller. You see that the C code is generated and you can now use the C code to deploy to your embedded devices.

    So far what we have seen is how you can include expert knowledge in the form of logical rules to create your fuzzy system. For such systems when the number of inputs or a number of outputs increase, the number of rules and number of parameters to configure also increases. So it becomes challenging to manually add all of these rules and membership function parameters. In such situations, there are two approaches you can follow. One is fuzzy trees and the other is fuzzy tuning.

    Let's first take a look at fuzzy trees. So using fuzzy trees, you can create complex fuzzy systems as a collection of smaller fuzzy systems. How it works is that instead of having one monolithic structure with all inputs and outputs, you would break your fuzzy system into smaller interconnected pieces where outputs of lower level system are connected as inputs to the high level fuzzy system.

    In the image on your screen, you can see that the output of FIS 1 is connected as an input to FIS 2. Even for this simple system with just three inputs, the number of possible parameters to configure reduces. And the number of parameters being smaller make fuzzy trees more computationally efficient and it also makes it easier to understand when compared with the standard fuzzy inference system with the same number of inputs and outputs.

    Now, there are different fuzzy tree structures that you can create based on your application. And the ones you see in the image on the right are the commonly used configurations. I won't be going deep into this topic today in the session, but for more information on this, I have included link to our documentation page at the end of the presentation. Please feel free to take a look at it.

    So that was fuzzy trees. Now let's take a look at fuzzy tuning. So another approach to handle the complexity of manually configuring a large set of rules and parameters is using fuzzy tuning where you automatically tune parameters from a data. Another way to think about it is when you already have collection of data from your system, which you would like to use for modeling.

    In that case, discerning or extracting membership function parameters from data could be a challenging task. So instead you can use the data to automatically tune the membership function parameters as well as the rules of your fuzzy system. Here the tuning process uses optimization techniques such as genetic algorithm and particle swarm optimization. I also want to point out that fuzzy tuning is applicable to both type one, type two fuzzy systems, as well as fuzzy trees.

    So now you might be thinking, wait, I'm using some data to create or train a system. So what is the difference between tuning a fuzzy system or just tuning or training a neural network? So how does this differ from neural networks?

    So the way it differs from neural networks is that neural networks are unexplained black box AI models where it's difficult to understand the decision making process of a neural network. But with fuzzy, the resulting fuzzy system is still rule based, and that makes it more explainable than neural networks.

    Now for applications where you already have neural networks implemented but you want to know the factors on which your neural network bases its decision on. For example, say for the same loan risk assessment or loan assessment problem that we briefly touched upon earlier. If a customer were to come back to you and ask you, why was I declined the loan? Could you tell me the exact reason?

    And if you were to do that with a neural network, it will be a very challenging task to pinpoint the reason or you might not be able to pinpoint the reason. Or on the other end, if you were to get unexpected results from neural network, you would be interested in knowing as to why you're getting this decision, or more importantly, how to go about improving this decision.

    Then in such situations, what you can do is you can use fuzzy tuning capabilities and create a fuzzy support system and use that along with your neural network to explain the functioning of a black box model through fuzzy system's explainable rule base. The image that you see there was from an example where the fuzzy logic was used to explain the functioning of an automotive lane keep assist system. And the rule base of the fuzzy system make it more explainable, and that way we were able to interpret the functioning of the black box model.

    Again, in this case, I won't be going deep into this topic. But for more information on this, I have included links here to the reference examples that show you how you can use fuzzy to explain working of a black box model. With that, we've touched upon the different types of fuzzy inference systems and the different techniques that you can use to create fuzzy systems based on your need, application, and the complexity.

    To quickly summarize what we've seen today, we saw how you can design, analyze, and evaluate fuzzy systems using the Fuzzy Logic Designer app. We also saw how you can integrate the designed fuzzy system into Simulink for system level simulation, system level testing across different scenarios. And once tested, we also saw how you can generate code for deployment to your embedded devices.

    We also briefly touched upon the different application areas where fuzzy logic can be used, mainly covering the broad areas of control design, intelligent decision making, signal processing, and more recently with the fuzzy tuning capabilities explainable AI. That is to explain AI based black box models. So these are the application areas where you can look to apply fuzzy logic using Fuzzy Logic Toolbox.

    I also want to point out that our customers have been successful using Fuzzy Logic Toolbox for a variety of applications. Cornell Bioacoustics used Fuzzy Logic Toolbox along with other products to detect and classify animal sounds in huge sets of acoustic data. Lufthansa Technik, on the other hand, used fuzzy logic for work scoping decisions. I have included links for your reference. Please feel free to take a look at them to learn more.

    I hope I was able to address these key takeaways. I want to leave you all with some call to action. You can get started with Fuzzy Logic Toolbox by downloading a free trial of the toolbox. You can then try out the examples that interest you the most from a variety of list of examples that we provide in our documentation. Additionally, you can also reach out to us for technical support, for guided evaluations, or for custom engagements to see how you can use fuzzy logic to fit in with your workflows or your specific application.

    Lastly, I also want to point out some additional resources that can help you learn more about fuzzy logic. We have tech talks from Brian Douglas and we also have a good list of demo videos that we've put out in showing how to use Fuzzy Logic Toolbox or capabilities from the toolbox to go about dealing with different applications.

    Lastly, I've also added links to the documentation on the different topics that we discussed today. Please check them out to learn more. This concludes the webinar. Thank you all for listening, and thank you for your time.

    Related Products