I made a saturation bolck with c-script, but it can’t work normally compared with the code in the help document, which is the inside code about saturation block in the library.
So can you tell me the difference about two c-scripts?
Your implementation does not behave correctly in minor timesteps. This is not obvious in the test model that you have attached, because the scope will only evaluate the output in major timesteps.
The difference becomes obvious if you feed the output of the C-Scripts to an integrator. With your implementation there is an integration error (the output of the integrator does not become 0 after one period), whereas the reference implementation works correctly (see attached model).
Thanks for you answer, but what I want to know is that what caused the difference? Is the using of the " if … switch…" ? after all, two c-scripts are so alike.
They are not alike at all. The correct one specifies the behavior in major and minor steps, the other one only the behavior in major steps. The reason why the behavior in minor steps must be specified is given in the help document that you have cited.