Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    3
    ReputationRep:
    ...

    Say I have this file, but how do I apply this algorithm to work on function I define? Where do I define the function? The command window? I have trouble making it work on some function, and I need to know how to apply it before I can start testing a bigger program I've written

    Offline

    11
    ReputationRep:
    Snapchat me sayitLoud2

    However, let me say here that what you have is a function. a function needs to be saved in a folder. This folder should be the one in which your workspace is currently opened to.

    To call this particular function, all you need to do is type exactly what you can see at the top of the function, well all of it except the word "function".

    So in this case, type

    >> res=BisectionMethod_Basic(f, an, bn)


    before you do this however, you should define what f, an and bn are. so something like this

    >>f = 4;
    >>an=59;
    >>bn=123;
    >> res=BisectionMethod_Basic(f, an, bn)

    That is how to call a function. it takes in inpputs f, an and bn and gives you output res






    (Original post by LeTroll)
    ...

    Say I have this file, but how do I apply this algorithm to work on function I define? Where do I define the function? The command window? I have trouble making it work on some function, and I need to know how to apply it before I can start testing a bigger program I've written

    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by Onlineslayer)
    Snapchat me sayitLoud2

    However, let me say here that what you have is a function. a function needs to be saved in a folder. This folder should be the one in which your workspace is currently opened to.

    To call this particular function, all you need to do is type exactly what you can see at the top of the function, well all of it except the word "function".

    So in this case, type

    >> res=BisectionMethod_Basic(f, an, bn)


    before you do this however, you should define what f, an and bn are. so something like this

    >>f = 4;
    >>an=59;
    >>bn=123;
    >> res=BisectionMethod_Basic(f, an, bn)

    That is how to call a function. it takes in inpputs f, an and bn and gives you output res
    Thanks, that makes sense, but f needs to be a function, and if I define it to be, say, >> f=x-exp(-x) then I get an error saying "Undefined function or variable x" ??
    Offline

    22
    ReputationRep:
    (Original post by LeTroll)
    Thanks, that makes sense, but f needs to be a function, and if I define it to be, say, >> f=x-exp(-x) then I get an error saying "Undefined function or variable x" ??
    One way to get past this is to have a seperate file in the same folder called f whose contents are

    function y = f(x)
    y = x-exp(x);
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by Zacken)
    One way to get past this is to have a seperate file in the same folder called f whose contents are

    function y = f(x)
    y = x-exp(x);
    Thx, I found a diff way around it

    Tho can u tell me how to suppress the 'ans' in the command window? Like I wrote my program and it all works nicely at the moment, but I want to output the answer with the fprintf command, which it does, but I also get the 'ans' right below it in the command window so then I have two of the same answer. How do I get rid off the 'ans'? The program in context is a root finding algo with the first line as 'function res = T(f, an, bn, convpar, maxiter)' and it goes around to find xn = (an+bn)/2 until the convergence parameter is satisfied. I put a res = xn; with the semicolon in the program after a suitable xn is achieved, but it still gives me the 'ans' output so I'm not sure where to place the semicolon
    Offline

    22
    ReputationRep:
    (Original post by LeTroll)
    Thx, I found a diff way around it

    Tho can u tell me how to suppress the 'ans' in the command window? Like I wrote my program and it all works nicely at the moment, but I want to output the answer with the fprintf command, which it does, but I also get the 'ans' right below it in the command window so then I have two of the same answer. How do I get rid off the 'ans'? The program in context is a root finding algo with the first line as 'function res = T(f, an, bn, convpar, maxiter)' and it goes around to find xn = (an+bn)/2 until the convergence parameter is satisfied. I put a res = xn; with the semicolon in the program after a suitable xn is achieved, but it still gives me the 'ans' output so I'm not sure where to place the semicolon
    The first line of your funcion shouldn't have the "res" in there. The res = means that the return type of the function is not null, so it outputs an ans. Instead just function Bisection... whatever should work. If you need the variable res to retain some information then do that in the program itself.
    Offline

    11
    ReputationRep:
    (Original post by LeTroll)
    Thx, I found a diff way around it

    Tho can u tell me how to suppress the 'ans' in the command window? Like I wrote my program and it all works nicely at the moment, but I want to output the answer with the fprintf command, which it does, but I also get the 'ans' right below it in the command window so then I have two of the same answer. How do I get rid off the 'ans'? The program in context is a root finding algo with the first line as 'function res = T(f, an, bn, convpar, maxiter)' and it goes around to find xn = (an+bn)/2 until the convergence parameter is satisfied. I put a res = xn; with the semicolon in the program after a suitable xn is achieved, but it still gives me the 'ans' output so I'm not sure where to place the semicolon
    To suppress the answer at every stage, put a semicolon at the end of every line.
    Including the line where you have >>res=an;
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Did TEF Bronze Award affect your UCAS choices?
    Useful resources

    Make your revision easier

    Maths

    Maths Forum posting guidelines

    Not sure where to post? Read the updated guidelines here

    Equations

    How to use LaTex

    Writing equations the easy way

    Student revising

    Study habits of A* students

    Top tips from students who have already aced their exams

    Study Planner

    Create your own Study Planner

    Never miss a deadline again

    Polling station sign

    Thinking about a maths degree?

    Chat with other maths applicants

    Can you help? Study help unanswered threads

    Groups associated with this forum:

    View associated groups
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.