June 28th, 2019, 02:17 AM  #1 
Math Team Joined: Jul 2013 From: काठमाडौं, नेपाल Posts: 903 Thanks: 62 Math Focus: सामान्य गणित  Matlab
How do I create a piecewise function? Suppose I want to define y = a , for t>=5 y = b, otherwise Last edited by skipjack; June 28th, 2019 at 05:30 AM. 
June 28th, 2019, 03:58 AM  #2 
Senior Member Joined: Sep 2015 From: USA Posts: 2,585 Thanks: 1430  
June 28th, 2019, 05:05 AM  #3 
Senior Member Joined: Feb 2016 From: Australia Posts: 1,839 Thanks: 653 Math Focus: Yet to find out. 
One good thing about matlab is its userfriendly documentation. Very easy to read in most cases..
Last edited by skipjack; June 28th, 2019 at 05:36 AM. 
June 28th, 2019, 06:54 AM  #4  
Math Team Joined: Jul 2013 From: काठमाडौं, नेपाल Posts: 903 Thanks: 62 Math Focus: सामान्य गणित  Quote: "Undefined function or variable 'piecewise'."  
June 28th, 2019, 08:24 AM  #5 
Senior Member Joined: Jun 2019 From: USA Posts: 309 Thanks: 160 
You don't need symbolic, since false == 0 and true == 1 and MATLAB doesn't mind mixing numeric types. y = b + (ab).* (t >= 5); 
June 28th, 2019, 08:44 AM  #6 
Math Team Joined: Jul 2013 From: काठमाडौं, नेपाल Posts: 903 Thanks: 62 Math Focus: सामान्य गणित 
Please see this

June 28th, 2019, 08:55 AM  #7 
Senior Member Joined: Jun 2019 From: USA Posts: 309 Thanks: 160 
Ah, if you're going to do it element by element, then it should be k(j). Of course, in that case, it seems more logical to just use an if/else statement. if (k(j) <= alpha) Vo(1,j) = Vo1(1,j); else Vo(1,j) = Vo2(1,j); end (Edit: I'm assuming t is a 2001element vector. By the way, IF your V variables are are singlerow arrays, you don't actually need the first index there. Vab(j) would work just as well.) Last edited by DarnItJimImAnEngineer; June 28th, 2019 at 09:01 AM. 
June 28th, 2019, 12:54 PM  #8  
Math Team Joined: Jul 2013 From: काठमाडौं, नेपाल Posts: 903 Thanks: 62 Math Focus: सामान्य गणित  Quote:
The problem is there is some logical error. Thank u so much for earlier insight.  
June 28th, 2019, 03:31 PM  #9 
Senior Member Joined: Jun 2019 From: USA Posts: 309 Thanks: 160 
It's hard to debug it without seeing the rest of your code, or at least the sizes of your variables. What are the dimensions of t, k, and alpha? If the a*(logical) + (ba)*(not logical) works, then the IF/ELSE version should work, as well. Also, just out of curiosity, did you actually study computer programming, or are you figuring it out as you go? 
June 30th, 2019, 06:28 AM  #10 
Math Team Joined: Jul 2013 From: काठमाडौं, नेपाल Posts: 903 Thanks: 62 Math Focus: सामान्य गणित 
Yes i have stidied some computer programming but not matlab specifically. Does this piecewise function really exist? 

Tags 
matlab 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Using Matlab  MathAboveMeth  Math Software  1  December 21st, 2016 10:06 AM 
matlab  Elenashcher  Math Software  1  April 5th, 2015 07:03 AM 
matlab  Prakhar  Math Software  1  August 20th, 2014 01:47 AM 
Need Matlab Help  stealth4933  Math Software  1  April 30th, 2014 02:37 AM 
Matlab Help!  Abhimint  Math Software  0  January 26th, 2013 06:47 PM 