Graphics Programs Reference
In-Depth Information
end
error('Too many iterations')
>>x=solve(2)
%numIternotprinted
x=
1.8955
>> [x,numIter] = solve(2) % numIter is printed
x=
1.8955
numIter =
4
>> format long
>>x=solve(2,1.0e-12)
%Solvingwithextraprecision
x=
1.89549426703398
>>
Evaluating Functions
Let usconsideraslightlydifferent version of the function solve shown below. The
expression for dx , namely
f ( x ), is now codedinthe function myfunc ,
so that solve contains a call to myfunc . This will work fine, provided that myfunc is
stored under the file name myfunc.m so thatMATLAB can find it.
=−
/
x
f ( x )
function [x,numIter] = solve(x,epsilon)
if nargin == 1; epsilon = 1.0e-6; end
for numIter = 1:30
dx = myfunc(x);
x=x+dx;
if abs(dx) < epsilon; return; end
end
error('Too many iterations')
functiony=myfunc(x)
y = -(sin(x) - 0.5*x)/(cos(x) - 0.5);
>>x=solve(2)
x=
1.8955
Search WWH ::




Custom Search