"step"-function

Hi,
Here, I just would like to create a step function wave that depends on x. I wonder why it displays 0 for both conditions? It shouldn't! Any help would be appreciated. Thanks!
#pragma rtGlobals=1     // Use modern global access method.
Function CreatingBConeData()
    KillWaves/A
    Variable theta = 39.5                   //Semi-included angle of cone (generally ~39.5 deg)
    Variable R = 40e-9                  //radius of spherical tip
    Variable b = R*cos(theta*pi/180)
    Variable eta = 0.5
    Variable hstar = (b^2)/(2.*R)
    Variable Stif = 1000
    Make/N=(100,1) Phorce; SetScale/I x, 0, 1e-6, Phorce
    variable index
    for(index=0; index<100; index+=1)
        if(index<50)        //if(x<0.5e-6)
            Phorce = (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5
        elseif (index>50)       //elseif(x>0.5e-6)
            Phorce = 0
        endif
    endfor
    print hstar
End.
I think this is closer to what you are looking for:

Function CreatingBConeData()
    Variable theta = 39.5                   //Semi-included angle of cone (generally ~39.5 deg)
    Variable R = 40e-9                  //radius of spherical tip
    Variable b = R*cos(theta*pi/180)
    Variable eta = 0.5
    Variable hstar = (b^2)/(2.*R)
    Variable Stif = 1000
    Make/O/N=(100,1) Phorce = 0; SetScale/I x, 0, 1e-6, Phorce
    variable index
    Phorce = (p < 50) ? (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5 : 0
    print hstar
End


You might want to read up on wave assignment. To do that, execute the following command on Igor's command line:
DisplayHelpTopic "Waveform Arithmetic and Assignment"
mssmtrainee:

Another simple one-liner step-function is using just a logic multiplier factor:
Make/O/N=(100) Phorce = 0; SetScale/I x, 0, 1e-6, Phorce
Phorce =  (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5 * (p<50)  // logic multiplier factor on index

You could also use (x<0.5e-6) as the multiplier. Note that if you don't really need Phorce defined as a 2D wave (albeit 100 by 1), you are probably better off with a 1D wave as shown.
To answer your original question of why the output is all zero, this:
    if(index<50)
        Phorce = (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5
    elseif (index>50)
        Phorce = 0
    endif


should be this:

    if(index<50)
        Phorce[index] = (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5
    else
        Phorce[index] = 0
    endif


Notice the use of "[index]" to set a particular point in the wave. You were setting every point in the wave each time through the loop.

I also changed your elseif to if because your code did nothing for index==50.

Even with my changes, I still get all zeros. If you change the complicated expression to 1, you will see that the basic logic is correct - you get a square wave. Thus I conclude that your expression is returning zero for all x.

hrodstein wrote:

    if(index<50)
        Phorce[index] = (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5
    else
        Phorce[index] = 0
    endif


Notice the use of "[index]" to set a particular point in the wave. You were setting every point in the wave each time through the loop.

I also changed your elseif to if because your code did nothing for index==50.

Even with my changes, I still get all zeros. If you change the complicated expression to 1, you will see that the basic logic is correct - you get a square wave. Thus I conclude that your expression is returning zero for all x.


There is an "x" in the calculation of the Phorce value. That's why I used a ternary assignment in my original answer.

Here is code that uses the loop and gives the correct result:
Function CreatingBConeData()
    Variable theta = 39.5                   //Semi-included angle of cone (generally ~39.5 deg)
    Variable R = 40e-9                  //radius of spherical tip
    Variable b = R*cos(theta*pi/180)
    Variable eta = 0.5
    Variable hstar = (b^2)/(2.*R)
    Variable Stif = 1000
    Make/O/N=(100,1) Phorce = 0; SetScale/I x, 0, 1e-6, Phorce
   
    // Using a loop for wave assignment (this is the slow way)
    variable index
    for(index=0; index<100; index+=1)
        if(index<50)
            Phorce[index] = (4/(3*(1-eta^2)))*sqrt(R)*Stif*pnt2x(Phorce, index)^1.5
        else
            Phorce[index] = 0
        endif
    endfor
   
    // Using a proper wave assignment statement (this is the faster way)
//  Phorce = (p < 50) ? (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5 : 0
    print hstar
End
There is something interesting happening here. With Howard's code I get the proper(?) non-zero result for
for(index=0; index<100; index+=1)
    if(index<50)        //  if(x<0.5e-6)
        Phorce[index][0] = (4/(3*(1-eta^2)))*sqrt(R)*Stif*x^1.5
    else   
        Phorce[index][0] = 0
    endif
endfor

Note the second wave dimension; remember that Phorce was defined as a 2D wave by mssmtrainee. I agree with Adam about the ambiguous value of 'x' in the wave element assignment, but it somehow seems to work with this version. Perhaps the 'index' value forces the proper scaled assignment of 'x' without explicit use of pnt2x().
Apparently the use of 2D syntax (phorce[index][0]=) cause x to be set when the righthand side is executed but the use of 1D syntax (phorce[index]=) does not.

Since this is a 1D application, a 1D wave should be created. So this:
Make/N=(100,1) Phorce

should be changed to:
Make/N=(100) Phorce