1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB function within function handle output

  1. Dec 7, 2017 #1
    1. The problem statement, all variables and given/known data
    MATLAB

    The funciton "rockdynamicstage" outputs a matrix 4 by x, where x is somthing around 300( varies).

    The funciton "rockdynamicstage" is within the funciton handle "sim" with an input of gama0. (PayMass,Masstages,Massfrac,Thstage,IspStage,ChangeTstage) ar parameters that are set earlier in the code.

    sim= @(gama0) rockdynamicstage(gama0,PayMass,Masstages,Massfrac,Thstage,IspStage,ChangeTstage);


    2. Relevant equations

    I need the "sim" to output the last element in the 4th row of the "rockdynamicstage". So that I get the last element in the 4th row in "rockdynamicstage" for a range of gama0s.

    How would I output it.
    3. The attempt at a solution
    I tried doing
    sim= @(gama0) rockdynamicstage(gama0,PayMass,Masstages,Massfrac,Thstage,IspStage,ChangeTstage)(4,end);

    similar to when using a regular matrix
     
  2. jcsd
  3. Dec 7, 2017 #2

    donpacino

    User Avatar
    Gold Member

    x= @(gama0) rockdynamicstage(gama0,PayMass,Masstages,Massfrac,Thstage,IspStage,ChangeTstage);
    sim=x(4,end);

    Or you can change the rockdynamicstage function
     
  4. Dec 7, 2017 #3
    But I need it to be a function handle. I need to be able to plug the function "sim" (that would ideally output the last element in the 4th row for a range of gama0s) into another function that finds the zero point.

    So essentially I need the function handle to output a (x,2) array of the gama0s and the 4th row last elements for the rockdynamicstage.
    output from the "sim" (or what you have as x) needs to be
    [ gama01, rockdynamicstage(4,end); gama02, rockdynamicstage(4,end); gama03, rockdynamicstage(4,end);......so on]
     
  5. Dec 7, 2017 #4

    donpacino

    User Avatar
    Gold Member

    well if you want to make it very complicated then you need to change rockdynamicstage
     
  6. Dec 7, 2017 #5

    donpacino

    User Avatar
    Gold Member

    you can use a simple for loop to move between cases, calling sim at each gama value
     
  7. Dec 7, 2017 #6
    This essentially is the outline but myrocketsim is rockdynamicstage. And the bisection method is used to find the zero
    upload_2017-12-7_15-6-52.png
     
  8. Dec 7, 2017 #7

    donpacino

    User Avatar
    Gold Member

    There is a key difference between the code you presented and the code in step 2
     
  9. Dec 7, 2017 #8
    What would that be?
     
  10. Dec 7, 2017 #9
    Is it the "get()" part
     
  11. Dec 7, 2017 #10

    donpacino

    User Avatar
    Gold Member

  12. Dec 7, 2017 #11

    donpacino

    User Avatar
    Gold Member

    I didn't realize get could extract elements of a matrix
     
  13. Dec 7, 2017 #12
    that syntax "get()" doesn't work though
     
  14. Dec 8, 2017 #13

    donpacino

    User Avatar
    Gold Member

    get is designed to work with objects. My guess is you are supposed to define myrocket sim (or equivalent) as an object
     
  15. Dec 8, 2017 #14

    donpacino

    User Avatar
    Gold Member

    Do you know for sure that your professor requires this architecture. It could be done very easily by using a loop or modifying the function calls.

    If not you need to modify the way your myrocketsim works
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Loading...