Help in writing a function file where the user defines a linear function

5 views (last 30 days)
I am meant to use the false-position method, and my function definition given by the instructor is:
function [root,fx,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin).
How do I plug my xl and xu into a user defined function in a loop? Can I do it in the M-file editor, or does the user define the function before hand? I have seen two different methods, but I feel like there might be a more intuitive way to do it.
:EDIT:
Here's my code so far:
function [root,fx,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)
% False Position Method = xl-(((xu-xl)*f(xl))/(f(xu)-f(xl)));
% falsepos uses the false position method to determine the roots of
% a user generated function.
%
% input:
% func = user generated function
% xl = guess of lower root
% xu = guess of upper root
% es = max allowed error
% maxit = maximum iterations of the function
% output:
% root = estimate of the true root of the function
% fx = function of x
% ea = approximate error
% iter = number of iterations gone through
if nargin<2|isempty(xl),xl=0.1;end
if nargin<3|isempty(xu),xu=0.2;end
if nargin<4|isempty(maxit),maxit=50;end
f=func;
fxl=f(xl);
fxu=f(xu);
xr=(xu+xl)/2
root=xl-(((xu-xl)*fxl)/(fxu-fxl));

Accepted Answer

dpb
dpb on 19 Jun 2014
...how do I plug my xl and xu into a user defined function in a loop?
All depends on how you want it to behave -- generally you would either write another script that calls the function and passes all the values. These could be stored internally and iterated over (not particularly flexible) or one could ask the user for input values or try to estimate them from some other set of information...it's really a choice depending on what you want (and the assignment requests, in this case).
  4 Comments
Chad
Chad on 29 Jun 2014
Edited: Chad on 29 Jun 2014
Well, the class is Numerical Methods, so it aint little kid stuff, but this is the prompt:
"Develop an M-file for the false-position method. Test it by solving Problem 1 (problem one has a function that calculates the drag coefficient needed so that an 80-kg bungee jumper has a velocity of 36 m/s after 4 s of free fall. This is the function that I presume is going to be used). So everyone is consistent use the following as the first line of your function.
function [root,fx,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)"
The linear function for as I wrote it out in MATlab goes like this (x=drag coeff): "(sqrt((g*m)/x)*tanh(sqrt((g*x)/m)*t))-v". Seeing how you described syntax for your linear fuction I might have a problem here as well, as I am using it to test the M-file. XL and XU are my lower and upper guesses for the drag coefficient.
I'm guessing that the notation "@(x)" tells matlab to hold onto the variable for later definition? I'm only really confused as to how MATlab takes the function from "s=input('Enter a functional form in x: ', s" and plugs a variable into it.
dpb
dpb on 29 Jun 2014
... the notation "@(x)" tells matlab to hold onto the variable for later definition?
No, the ampersand is the keyword for an anonymous function in Matlab; the (x) is the dummy argument for the function defined as the string built from the input string function requested prepended by '@(x)' the whole string to be converted to a function handle for later execution by str2func
But, from the description of the problem, I think you are definitely over-thinking the intent by a sizable margin--all it's asking is for you to write a function with the given footprint that solves the one given equation and the two bounding values are given as arguments.
You could simply ask for them individually and pass those returned values to the function.
What is wanted/needed in the function falsepos for the func argument is simply the function defined in the m-file for the evaluation of the problem as given.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!