MATLAB Answers

Simulink bug - adding a PID only to scope creates large fluctuations

14 ビュー (過去 30 日間)
Nitai Sasson
Nitai Sasson 2020 年 6 月 13 日
コメント済み: Fangjun Jiang 2020 年 6 月 15 日
I am a student doing a homework assignment and encountered this bug. Is this a known bug? Couldn't find anything when I searched for it.
Background:
I'm trying to implement a PID control loop. However, I noticed that the PID block's transfer function has an extra parameter (N) that I wasn't taught. I looked it up online and I understand why it's there, but nevertheless, I noticed that the "derivative" block doesn't require the same parameter (it requires a "c" parameter which defaults to inf, whereas N can't be set to inf) so I decided to implement PID "manually" using the derivative block, and compare the results with the built-in PID block using the default N and c values.
I created this system:
With the PID block and scope deleted or commented out, the "hand-crafted" PID system works as expected:
However, when I enable the PID and scope blocks, even though they should not affect any other part of the system, I get massively different results:
I assume that somehow the existence of the PID block triggered some (bad) optimization for the derivative block, creating this unexpected result. This is just conjecture of course.
I've attached the .slx file to this post so you can inspect this behavior yourself (not all values and parameters are visible in the screenshot).
This is a pretty crazy bug. Luckily it doesn't actually mess up my usage, but if I were to encounter it in any real-world usage it would be horrifying. How would I work around this?

  2 件のコメント

Jonas
Jonas 2020 年 6 月 15 日
I can not view your Simulink file because I am still using version R2019a, but I would inspect the solver settings. Is the solver using a bigger sampling time with the added PID(s) block? Can you try to make sure the sampling time is kept small?
Nitai Sasson
Nitai Sasson 2020 年 6 月 15 日
I don't know how to check. I've exported it to R2019a though, hopefully it'll exhibit the same behavior and you can play around with it.

サインインしてコメントする。

採用された回答

Jonas
Jonas 2020 年 6 月 15 日
編集済み: Jonas 2020 年 6 月 15 日
Your solver settings change when you uncomment the PID(s). If you fix the solver to ode45 for example, the simulation runs ok. You can click on the link at the bottom right corner of your Simulink window to change it.
I think it is a good time for you to learn more about the solver settings as it can have a significant impact on the simulation results as you can see.

  0 件のコメント

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2020 年 6 月 15 日
編集済み: Fangjun Jiang 2020 年 6 月 15 日
It is not a bug. Don't claim it is a bug when you have no idea. You need to learn how digital simulation solves an analog problem (derivative). Your constructed PID controller is a therotically ideal controller which can't be implemented in real world. While the Simulink PID controller is a realistic one that can be implemented in a digital controller.
If you don't know how to check the solver settings, then I bet the settings are all auto. The Simulink solver is looking into the stiffness of the simulation and adjusts the solver, the tolerance and the step size. That is the cause of the fluctuations. You could comment out your own PID controller and connect the Simulink PID controller and see what happens using the default auto settings. Most likely, it is your ideal derivative that causes the stiffness of the simulation.
Press Ctrl+E at your model, click "Solver" at the left column and look into all the settings. Click "Help" button to bring up the document. Or, hover over any one item, e.g. "Max step size", right click, then select "What's this?"

  3 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 15 日
+1, "slam dunk"
Nitai Sasson
Nitai Sasson 2020 年 6 月 15 日
You're right, the settings are all auto. But the bug I was complaining about (okay - possibly not a bug, but I'd appreciate a better explanation than "you have no idea") is that the fluctuations only appear when I add something that doesn't affect the system (a PID controller that only outputs to a scope). If the fluctuations had appeared both with and without it, I would have assumed it was my fault one way or another for trying to be clever.
Before making this question, I looked up the derivative block online and found this page: https://www.mathworks.com/help/simulink/slref/derivative.html
It basically says it approximates the derivative as equal to the change from one time step to the next, divided by the length of that time step. That's okay... And it shouldn't cause the behavior I see here. The actual derivative is never bigger than 2 in absolute value after t=400, and the approximation should never be bigger than the actual derivative. There are no discontinuities or weird things that would cause it to be completely wrong.
Fangjun Jiang
Fangjun Jiang 2020 年 6 月 15 日
  1. Using auto solver, either comment out your own PID controoler, or comment out the Simulink PID controller, the simulation runs properly. The fluctuation only happens when both PID controllers are simulated. It is like the auto focus of a camera. It is tough to make the face of the person and the background mountain landscape focused at the same time.
  2. I am suprised that changing solver to ode45 would make the simulation run properly. In this sense, the Simulink "auto solver selection" is kind of "buggy".

サインインしてコメントする。

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by