My Math Forum Matlab

 Math Software Math Software - Mathematica, Matlab, Calculators, Graphing Software

 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 This isn't Google. https://www.mathworks.com/help/symbolic/piecewise.html Thanks from Joppy
 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 user-friendly 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:
 Originally Posted by romsek This isn't Google. https://www.mathworks.com/help/symbolic/piecewise.html
I tried it earlier but it says
"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 + (a-b).* (t >= 5); Thanks from MATHEMATICIAN
June 28th, 2019, 08:44 AM   #6
Math Team

Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 903
Thanks: 62

Math Focus: सामान्य गणित
Attached Images
 20190628_221130.jpg (95.0 KB, 4 views)

 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 2001-element vector. By the way, IF your V variables are are single-row arrays, you don't actually need the first index there. Vab(j) would work just as well.) Thanks from Greens 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:
 Of course, in that case, it seems more logical to just use an if/else statement.
IF ELSE doesn't work for some reason.
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) + (b-a)*(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 Linear Mode

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

 Contact - Home - Forums - Cryptocurrency Forum - Top