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

Click For Summary

Discussion Overview

The discussion revolves around implementing conditional logic using if/else statements in MATLAB to compute a variable f based on an array of time values. Participants explore different coding approaches to apply these conditions to each element of the array.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes a need to compute f based on time t using conditional statements but encounters errors when implementing it in a function.
  • Another participant points out that the use of "otherwise" is incorrect in if-elseif-else statements and suggests correcting the conditional expressions.
  • A later reply indicates that the function does not return an array of f values and suggests using a for loop to iterate through the array of times instead.
  • One participant proposes defining the time array differently and provides a working example using a for loop to apply the conditions to each element of the time array.
  • Another participant confirms that the provided solution works effectively for their needs.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the initial code, but there are differing views on the best approach to implement the solution, with some favoring array manipulation and others suggesting iterative methods.

Contextual Notes

Participants mention issues related to the definition of variables and the structure of conditional statements, indicating potential limitations in understanding MATLAB syntax and function behavior.

Who May Find This Useful

Individuals working with MATLAB who need to apply conditional logic to arrays, particularly in the context of programming for mathematical or engineering applications.

oceanspiral20
Messages
4
Reaction score
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
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
 
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?
 
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.
 
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:
Hi Ben H!

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

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K