Help in writing a function file where the user defines a linear function
Show older comments
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
More Answers (0)
Categories
Find more on Programming 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!