15.11.2014 Views

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Minimizing Functions and Finding Zeros<br />

model = @expfun;<br />

estimates = fminsearch(model, start_point);<br />

% expfun accepts curve parameters as inputs, and outputs sse,<br />

% the sum <strong>of</strong> squares error for A * exp(-lambda * xdata) - ydata,<br />

% and the FittedCurve. FMINSEARCH only needs sse, but we want to<br />

% plot the FittedCurve at the end.<br />

function [sse, FittedCurve] = expfun(params)<br />

A = params(1);<br />

lambda = params(2);<br />

FittedCurve = A .* exp(-lambda * xdata);<br />

ErrorVector = FittedCurve - ydata;<br />

sse = sum(ErrorVector .^ 2);<br />

end<br />

end<br />

The M-file calls the function fminsearch, which find parameters A and lambda<br />

that minimize the sum <strong>of</strong> squares <strong>of</strong> the differences between the data and the<br />

exponential function A*exp(-lambda*t). The nested function expfun computes<br />

the sum <strong>of</strong> squares.<br />

Running the Example<br />

To run the example, first create some random data to fit. The following<br />

commands create random data that is approximately exponential with<br />

parameters A = 40 and lambda = .5.<br />

xdata = (0:.1:10)';<br />

ydata = 40 * exp(-.5 * xdata) + randn(size(xdata));<br />

To fit an exponential function to the data, enter<br />

[estimates, model] = fitcurvedemo(xdata,ydata)<br />

This returns estimates for the parameters A and lambda,<br />

estimates =<br />

40.1334 0.5025<br />

and a function handle, model, to the function that computes the exponential<br />

function A*exp(-lambda*t).<br />

4-11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!