MATLAB Answers

# Linearization error "NaN's cannot be converted to logicals"

146 views (last 30 days)
I get the error. " NaN's cannot be converted to Logicals" when trying to linearize my model. The model runs actually with no problem. the problem arises only when linearizing the model in control design. I don't know what exactly simulink does during linearazation that causes this error.

#### 2 Comments

Ameer Hamza on 9 Aug 2018
Can you give an example of your model?
Walter Roberson on 9 Aug 2018

Sign in to comment.

### Accepted Answer

Adam Danz on 9 Aug 2018
This will replicate your error:
v = [1 0 1 1 0 NaN 0 1 0 0];
logical(v)
Error using logical
NaN's cannot be converted to logicals.
My guess is that you're using logical() to convert a variable with 0s and 1s that also have NaN values.
If that's the case, you need to decide whether NaNs should be a 0 or a 1. In this example, I set NaNs to 0.
v = [1 0 1 1 0 NaN 0 1 0 0];
v(isnan(v)) = 0;
logical(v)
Or you can remove the NaNs
v = [1 0 1 1 0 NaN 0 1 0 0];
v(isnan(v)) = [];
logical(v)

#### 4 Comments

Show 1 older comment
Adam Danz on 9 Aug 2018
As @Walter alluded to in the comment section under your question, you'll need to provide much more detail for us to blindly troubleshoot your problem.
Hossein Rezazadeh on 9 Aug 2018
Ok I got it. Apparently during the linearazation some numerical perturbation is fed to the block as input which causes "NAN" at the block output and this was triggering this error. adding v(isnan(v)) = []; to the block output solved the problem.
Tim on 8 Dec 2018
Hi, could you please explain how exactly you added v(isnan(v)) = []; to the block output?

Sign in to comment.

### More Answers (1)

walid benchouche on 4 Jan 2019
hello guys, can you please help me , i get the same error on model predictive control when linearizing Rc=0.15

#### 2 Comments

Hossein Rezazadeh on 4 Jan 2019
This happens in MPC when your model sends "NaN" to the "mo" inport of the MPC block.
As you have integrators in your model they should be responsible for "NaNs".
Either change the operating point at which you are linearizing the model or change the solver or max step size.
Apicha Kittirattanachai on 21 Aug 2020
please kindly explain "integrators in your model they should be responsible for "NaNs" "
How are integrators setting for response the NaN value ?
My problem is appear in mpc toolbox when linearize of toolbox.
Thankyou.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by