MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Giorgos on 6 Jan 2014

Hello, Im trying to make a simulink model for a project have. What Im trying to do is to control the values from sxitch2 based on the values imported from switch1, the function in the block has the following code:

function y =rounding(x) mintol=0.5; if abs(x)<mintol y=0; else y=x; end end

What I have noticed is that the time delay blocks initial value is zero "0" thus every new value that enters gets set to zero. Thus not able to lock the switch. I have tried to remove the unit block but I get the following error message:

"Simulink cannot solve the algebraic loop containing 'test/MATLAB Function2' at time 0.0 using the TrustRegion-based algorithm due to one of the following reasons: the model is ill-defined i.e., the system equations do not have a solution; or the nonlinear equation solver failed to converge due to numerical issues. To rule out solver convergence as the cause of this error, either

- switch to LineSearch-based algorithm using set_param('test','AlgebraicLoopSolver','LineSearch')
- reducing the VariableStepDiscrete solver RelTol parameter so that the solver takes smaller time steps. If the error persists in spite of the above changes, then the model is likely ill-defined and requires modification."

*No products are associated with this question.*

Answer by A Jenkins on 6 Jan 2014

Accepted answer

You must have an initial value for switch2 so that it knows which of the [111] or [000] to start with. Therefore you must have an initial result of your AND block, and an initial value of your unit delay blocks. (You could move and combine the unit delay right before your switch2 if that helps you think about it.)

That said, your values are not flip-flopping because of your initial value. Imagine you initially start with the switch2 "on" so that [111] is fed forward. Then since [111] is not equal to 0, the result of the AND will be 0 and your switch will turn "off", and select [000] next time. Then [000] is equal to 0 so result of the AND will be 1 and your switch will turn back "on" and select [111].

Perhaps you should explain what you are trying to do. Also, you might want to look into the latches and flip-flop blocks.

Answer by Azzi Abdelmalek on 6 Jan 2014

I'am not following you, the initial value of the unit delay block is used, only at the start of your simulation, and you can set it to whatever you want.

Answer by Giorgos on 6 Jan 2014

The thing is,

that the feedback goes to the unit block, initiating a zero value and thus over-writing the current value, causing the switch to go on and off instead of locking on one value.

As you can see from the attached graph the values after the time-delay block alternate instead of obtaining a fixed value.

Do you know why this happens?

I have attached the input scope graph as also the graph after the function block.

## 0 Comments