You are Here: Home >< Maths

# MATLAB Algorithm coding HELP watch

1. ...

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

2. 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

3. (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 needs to be a function, and if I define it to be, say, then I get an error saying "Undefined function or variable " ??
4. (Original post by LeTroll)
Thanks, that makes sense, but needs to be a function, and if I define it to be, say, then I get an error saying "Undefined function or variable " ??
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);
5. (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
6. (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.
7. (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;

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 5, 2017
Today on TSR

### He lied about his age

Thought he was 19... really he's 14

### University open days

Wed, 25 Jul '18
2. University of Buckingham
Wed, 25 Jul '18
3. Bournemouth University
Wed, 1 Aug '18
Poll
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams