Hi!(adsbygoogle = window.adsbygoogle || []).push({});

I have an array that is 1 column by many rows, of which each element represents a time. I want to find variable f at each time, where f is defined as follows:

if t < tp

f=(sin((pi*t)/(2*tp)))^2;

if tp < t < ts

f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;

otherwise f=0

I've tried defining f as above in a .m function file (called elastance), and then using f=arrayfun(elastance,t) to find f at each time, but that doesn't work (It says, "Error in ==> elastance at 5, if t < tp".) Just typing the above function directly into the command window doesn't work either, whether or not I put it in terms of t or (t.).

Does anyone have any suggestions? Thanks!

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Apply if/else if to each element in array in MATLAB

**Physics Forums | Science Articles, Homework Help, Discussion**