The Student Room Group

MATLAB Algorithm coding HELP

...

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

(edited 6 years ago)
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

Reply 2
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 ff needs to be a function, and if I define it to be, say, >>f=xexp(x)>> f=x-exp(-x) then I get an error saying "Undefined function or variable xx" ??
Reply 3
Original post by LeTroll
Thanks, that makes sense, but ff needs to be a function, and if I define it to be, say, >>f=xexp(x)>> f=x-exp(-x) then I get an error saying "Undefined function or variable xx" ??


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);
Reply 4
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
Reply 5
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.
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;

Quick Reply

Latest