IF statement on LTspice

Discussion in 'Power Electronics' started by Firenze02, Mar 8, 2019.

  1. Firenze02

    Thread Starter Member

    Sep 1, 2018
    32
    0
    Hi,

    I would like to create a variable resistor as following:

    R2=1 if i(R2)<1
    R2=2 if 1<i(R2)<10
    R2=3 if i(R2)>10

    I know that the sintax is:

    R=if(condition,valueiftrue,valueiffalse)

    What about if I have three intervals?

    The .asc file and the IF statement law is also shown in the attached files.

    Which is the correct sintax to do that?

    Thank you.
     
  2. eetech00

    Senior Member

    Jun 8, 2013
    1,472
    289
    HI

    The statements are incomplete.

    Write them in "if then else" pseudo code so we understand what you want.

    eT
     
  3. Firenze02

    Thread Starter Member

    Sep 1, 2018
    32
    0
    Dear eetech,

    I would like to "translate" in LTspice the following code:

    if i(R2)<1 {
    R2=1
    }

    if i(R2)>=1 && I(R2)<=10 {
    R2=2
    }

    else{
    R2=3
    }
     
  4. eetech00

    Senior Member

    Jun 8, 2013
    1,472
    289
    Ok...I’m reading those as two “if” statements OR’d together right?
     
  5. Firenze02

    Thread Starter Member

    Sep 1, 2018
    32
    0
    Yes, the interval are the following:

    R2=1 if i(R2)<1
    R2=2 if 1<i(R2)<10
    R2=3 if i(R2)>10

    Thank You for any suggestions.
     
  6. eetech00

    Senior Member

    Jun 8, 2013
    1,472
    289
    See below.

    In the .func statement, the "if" statement has to resolve to a number. I've added 0.5 at the end.
    In the "Limit" statement, I've added arbitrary Min and Max values, 0.1,12, respectively. A discontinuity will occur if not used because at the start of the simulation R equals nothing (NaN). This will produce a "time step too small" error. The "Limit" statement will keep R2 equal to some value between 0.1 and 12.

    eT

    upload_2019-3-8_14-33-51.png
     
  7. Firenze02

    Thread Starter Member

    Sep 1, 2018
    32
    0
    Thank you eetech,

    Finally I was able to simulate the circuit that I wanted, but unfortunately it doesn't work.

    It gives me the following message:

    Analysis: Time step too small, trouble with node v4.

    Please find attached the .asc file.

    Can you help me?

    Thank you.
     
  8. eetech00

    Senior Member

    Jun 8, 2013
    1,472
    289
    Hi

    See below.

    upload_2019-3-9_7-3-41.png
     
Loading...