My Math Forum  

Go Back   My Math Forum > Math Forums > Math Software

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


Thanks Tree3Thanks
Reply
 
LinkBack Thread Tools Display Modes
June 28th, 2019, 02:17 AM   #1
Math Team
 
MATHEMATICIAN's Avatar
 
Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 901
Thanks: 61

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.
MATHEMATICIAN is offline  
 
June 28th, 2019, 03:58 AM   #2
Senior Member
 
romsek's Avatar
 
Joined: Sep 2015
From: USA

Posts: 2,496
Thanks: 1371

This isn't Google.

https://www.mathworks.com/help/symbolic/piecewise.html
Thanks from Joppy
romsek is online now  
June 28th, 2019, 05:05 AM   #3
Senior Member
 
Joined: Feb 2016
From: Australia

Posts: 1,830
Thanks: 648

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.
Joppy is offline  
June 28th, 2019, 06:54 AM   #4
Math Team
 
MATHEMATICIAN's Avatar
 
Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 901
Thanks: 61

Math Focus: सामान्य गणित
Quote:
Originally Posted by romsek View Post
I tried it earlier but it says
"Undefined function or variable 'piecewise'."
MATHEMATICIAN is offline  
June 28th, 2019, 08:24 AM   #5
Member
 
Joined: Jun 2019
From: USA

Posts: 69
Thanks: 27

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
DarnItJimImAnEngineer is offline  
June 28th, 2019, 08:44 AM   #6
Math Team
 
MATHEMATICIAN's Avatar
 
Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 901
Thanks: 61

Math Focus: सामान्य गणित
Please see this
Attached Images
File Type: jpg 20190628_221130.jpg (95.0 KB, 4 views)
MATHEMATICIAN is offline  
June 28th, 2019, 08:55 AM   #7
Member
 
Joined: Jun 2019
From: USA

Posts: 69
Thanks: 27

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.
DarnItJimImAnEngineer is offline  
June 28th, 2019, 12:54 PM   #8
Math Team
 
MATHEMATICIAN's Avatar
 
Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 901
Thanks: 61

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.
MATHEMATICIAN is offline  
June 28th, 2019, 03:31 PM   #9
Member
 
Joined: Jun 2019
From: USA

Posts: 69
Thanks: 27

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?
DarnItJimImAnEngineer is offline  
June 30th, 2019, 06:28 AM   #10
Math Team
 
MATHEMATICIAN's Avatar
 
Joined: Jul 2013
From: काठमाडौं, नेपाल

Posts: 901
Thanks: 61

Math Focus: सामान्य गणित
Yes i have stidied some computer programming but not matlab specifically.

Does this piecewise function really exist?
MATHEMATICIAN is offline  
Reply

  My Math Forum > Math Forums > Math Software

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





Copyright © 2019 My Math Forum. All rights reserved.