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

In summary, the author is trying to find a time function at each time step in an array. However, when used standalone, the function does not work. The author fixes the errors and tries again, but still cannot get the function to work. They then describe a standard approach for finding a function at each time step using for loops and arrays. They also provide a complete code example.
  • #1
oceanspiral20
5
0
Hi!
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 news on Phys.org
  • #2
oceanspiral20 said:
Hi!
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!
I see several things that might be causing your problems:
othewise is used in switch statements, not in if - elseif - else statements.
Your 2nd if statement was testing a malformed expression (i.e., tp < t < ts). In most programming languages this should be made into two expressions.

The changes below might fix your problems.

Code:
if t < tp
    f=(sin((pi*t)/(2*tp)))^2;
elseif if tp < t & t < ts
    f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0
end
 
  • #3
Hi, thanks for your reply!

I did fix those errors, but still can't get it to work and apply to each element in the array. I've done it both just entering this code into the command window, and it only returns one element f=0 instead of f values for each time step in the array... Here's the code I'm using:

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
elseif tp < t & t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0
end

Also tried making a separate function with it and using arrayfun, as follows:

function f=elastance2(t)
tp=.35;
ts=.8;

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
elseif tp < t && t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0;
end

and in the command window:
f=arrayfun(elastance2,t)

but I get the following error:

? Input argument "t" is undefined.

Error in ==> elastance2 at 5
if t < tp


t is the array of times; if I type t and enter the array comes up, so I'm not sure why it is undefined. Does someone have any advice?
 
  • #4
your function works fine when i plug it in but you seem to have some errors?...is f intended to be an output array corresponding to your functions?

you need to use the . operator for array manipulation and the zeros and size functions for your else condition...

did you try defining t inside to see if the function works...or the stupid question is if you defined it before calling the function. try using a different letter encase yoru using two different letters.
 
  • #5
Hi oceanspiral20,
The following may help you:
I will define an array for time t, which will be many columns in one row, the opposite to what you said but this is pretty standard. Then using a for loop, we look at each t and check what conditions it satisfies etc. I have had to make up some constants which should be similar to yours. Here's the code:
%________________________________________________________
clear all
t = 0:0.01:0.2;
tp = 0.09;
ts = 0.15;
for h = 1:length(t)
if t(h) <= tp
f(h)=(sin((pi*t(h))/(2*tp)))^2;
elseif t(h) > tp & t(h) <= ts
f(h)=(cos((pi*(t(h)-tp))/(2*(ts-tp))))^2;
else
f(h)=0;
end
end
%_____________________________________________________

I have set tp as less than ts as is evident from your post. The time range is 0 to 0.2 seconds. So, when t is less than tp, it applies the first expression involving sin, when tp < t < ts, it applies the second expression invoving cos and when t is greater than ts (this addresses your 'otherwise' condition) it sets f = 0. Note the use of (h) to indicate looking at individual array elements.
Just set your values for t, tp and ts and it should work fine.

This code should do what you want when used independently, however if you are using it within another script you may need to make some adjusments. In other words, this doesn't need any defined functions to work.
 
Last edited:
  • #6
Hi Ben H!

Thank you so much for your reply, that worked beautifully! :)
 

What is the purpose of using "if/else if" statements in MATLAB?

The purpose of using "if/else if" statements in MATLAB is to control the flow of a program based on certain conditions. These statements allow the program to make decisions and execute different actions depending on the input or data being processed.

How do you apply "if/else if" to each element in an array in MATLAB?

To apply "if/else if" to each element in an array in MATLAB, you can use a for loop to iterate through each element and use the "if/else if" statements to check the conditions for each element. This allows you to perform different actions on each element depending on its value or properties.

Can you use multiple "if/else if" statements in a single line of code in MATLAB?

Yes, you can use multiple "if/else if" statements in a single line of code in MATLAB by separating them with semicolons. However, this may make the code less readable and harder to debug, so it is generally recommended to use separate lines for each "if/else if" statement.

What happens if none of the conditions in an "if/else if" statement are true?

If none of the conditions in an "if/else if" statement are true, then the code inside the "else" statement (if present) will be executed. If there is no "else" statement, then the program will simply move on to the next line of code after the "if/else if" statement.

Can you nest "if/else if" statements in MATLAB?

Yes, you can nest "if/else if" statements in MATLAB by placing one set of statements inside another set of statements. However, it is important to keep the code organized and easy to read to avoid confusion and errors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
7
Views
835
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top