Info

This question is closed. Reopen it to edit or answer.

Working with inputs in functions

2 views (last 30 days)
Renan Portela
Renan Portela on 17 Mar 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
I have got a problem. I need to code a bisection program in Matlab but I need to make it useful for any mathematical function the user wants. Does anyone know how to write a code without defining the mathematical function (leaving the option for the user) on which the program will work on?
  2 Comments
Stephen23
Stephen23 on 17 Mar 2018
Edited: Stephen23 on 17 Mar 2018
That is what function handles are for! Either get the user to:
  • define a function handle, or
  • define a string (in MATLAB syntax) and convert this using str2func.
Renan Portela
Renan Portela on 17 Mar 2018
Edited: per isakson on 18 Mar 2018
how should i code in order to make it possible? how should I code to allow the user type in the function ? So far I wrote this code below but it works just for one single function which is written below.
clc
fprintf(' itteration a b lambda mi f(lamda) f(mi) \n')
clear
f=@(x) x^2 + 2*x;
format short
eps_abs = 1e-1;
eps_step = 1e-1;
a = -3;
b = 5.0;
iter=0;
max = 15;
datasave=[];

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!