% Written by Jiwon Seo (jwseo@stanford.edu, jiwons@gmail.com) % for EE 265 class (2/22/2008) % Press F5 to run this code. % F5 is one of the most useful shortcuts in Matlab. function SolveByBisection() % This code is for solving a general 'f(x) == 0' equation % using the Bisection method, which is usually taught in the % first class of Numerical Analysis. % % After learning many sophisticated algorithms in the Numerical % Analysis class during my undergrad in Aerospace Engineering % at KAIST (Korea Advanced Institute of Science and Technology), % this simple Bisection algorithm has been my favorite for % about 10 years in many practical situations. % % This algorithm is simple and slow but numerically stable, % so you can always find the solution if you selected % search interval correctly. Slowness is not a problem % in most cases because computers are so fast these days % and you can get a solution within a second anyway. :) clc; % Clear screen. close all; % Close all existing figures. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Define your search interval. searchFrom = 5; searchTo = 30; % This value determines the resolution of your solution. delta = 0.00001; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plot y = f(x) graph in your search interval and check % zero crossings, which helps you to figure out if your % interval is adequately chosen. % % If there's no zero crossing in your search interval, % your final solution will be incorrect. You may want % to stop running by pressing Ctrl+C if this function is % running more than several seconds. % Multiple zero crossings should also be avoided, but you % can still find all solutions one by one. :) x = linspace(searchFrom, searchTo, 1000); y = f(x); plot(x, y); hold on; x = [searchFrom, searchTo]; y = [0, 0]; plot(x, y, 'r'); xlim([searchFrom, searchTo]); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solve the equation. You don't need to change this part. % Just change the function definition at the end of this file. while abs(searchFrom - searchTo) > delta middlePoint = (searchFrom + searchTo)/2; f_searchFrom = f(searchFrom); f_middlePoint = f(middlePoint); if (f_searchFrom * f_middlePoint) <= 0 searchTo = middlePoint; else searchFrom = middlePoint; end end format long; answer = searchFrom %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Many engineers don't know how to make multiple functions % within one 'M file', so their folders usually have % a lot of small M files with single function. -_-; % % Matlab does provide a smarter way to help structured % programming and you should know about this if you're % educated at Stanford!! (B university in Bay Area does % not necessarily teach this. :P) % % The following function is valid within this M file only, % which is what we exactly want. % However, your main function should start with a proper % function definition as this file, otherwise you can't % define multiple functions in one M file. % That's the key point! function y = f(x) % Equation should be in the form of 'f(x) == 0'. % The output 'y' has to be real value. % The input 'x' could be a vector, so you should use % .^ operation instead of ^ operation. y = 0.456 * (x-15).^3 + 0.786*(x-10) - 3.685;