How do i write a matlab code that asks to input a funtion, and then uses that function in the rest of the program?

95 Views Asked by At

I want to write a matlab code that asks for us to enter a function, number of iterations and two endpoint values , and then uses bisection method to solve for the roots of the function. I have looked at alot of matlab codes for the bisection method but they all use the function already defined in the program.

1

There are 1 best solutions below

0
On

Let's take this example (thx, Moo). It defines the function f at the end. Delete that bit, and modify the first line to be function c = bisect(f, a, b, delta).

This new bisect can be called with something like

f = @(x)cos(x); disp(bisect(f, a, b, delta))

or with

disp(bisect(@f, a, b, delta))

if f is defined in a file f.m.

Look for topics 'anonymous functions' and 'function handles' in MATLAB for more.