Does anyone know how to use matlab to do bisection methods for numerical methods???
Please help.
AnimeGalleries [dot] Net | AnimeWallpapers [dot] Com | AnimeLyrics [dot] Com | AnimePedia [dot] Com | AnimeGlobe [dot] Com |
Does anyone know how to use matlab to do bisection methods for numerical methods???
Please help.
[s]Which do you understand the least? The theory behind the bisection method, or the matlab scripting?[/s]
EDIT:
Matlab might have a better root solver than bisection. Here's my octave implementation
Example use:Code:function [x,nsteps] = bisect(func,a,b,epsilon) funcstr = ['y=',func,';']; x = a; eval(funcstr); ya = y; x = b; eval(funcstr); yb = y; % Does it satisfy the intermediate value theorem? if (ya > 0 & yb > 0) | (ya < 0 & yb < 0) % Nope nsteps = -1; return; end % From mathworld nsteps = ceil(log2(b-a)-log2(epsilon)); for i=1:nsteps mid = 0.5*(a+b); x = mid; eval(funcstr); ymid = y; % Does the first bisected interval satisfy the intermediate value theorem? if (ymid > 0 & ya < 0) | (ymid < 0 & ya > 0) b = mid; yb = ymid; else % Nope, so it must be the second interval a = mid; ya = ymid; end end x = 0.5*(a+b);
You may have to tweak it a little for Matlab.Code:octave-3.2.4.exe:30> [y,nsteps] = bisect("x^2-2",1,2,1e-16) y = 1.4142 nsteps = 54 octave-3.2.4.exe:31> [y,nsteps] = bisect("x^2-2",2,5,1e-16) y = 5 nsteps = -1
Last edited by nslay; 03-10-2014 at 07:45 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks