Anyone know how to use matlab?

Soldato
Joined
7 Jan 2003
Posts
4,458
Location
Gold Coast, Australia
Ive got a question to do which involves newtons method, which is.

X(n+1) = Xn - f(Xn)/f^'(Xn)

Basically ive got to right a matlab fucntion with inputd b,c,d where they are arbitary real numbers and X0 which is an arbitary real or complex number, which produces an estimate to this equation.

y=x^3+4*x^2+6*x+10

I have used the editor to write equations for f(x) and f^'(x)

function y=f(x)
y=x^3+4*x^2+6*x+10

function y=fdash(x)
y=3x^2+8*x+6

I just dont understand how to execute them to work out the problem, anyone good with this program?
 
Well you'll need some kind of loop since presumably you'll want to keep doing the X(n+1) = Xn - f(Xn)/f^'(Xn) equation until it converges to a given accuracy. A while loop would probably be your best bet there. The help in matlab seems to be quite good so you should be able to look that up on there. Basically you'll want to keep a variable that holds your latest estimate of X and update that with the newest one every run through of the loop.

I'm guessing matlab knows how to deal with complex numbers. If it doesn't that would make things a bit more tricky.
 
Yer i know, i mean it works fine manually. We just need to get a forumula like that working and we have currently been sat here 3 and a half hours today and got nowwhere.
 
function [X_est,E] = newton(x0,epsilon)
%
% function [X_est,E] = newton(x0,epsilon)
%
% This function finds a solution to the equation
%
% f(x) = 0 (1)
%
% by Newton's method.
%
% Inputs:
%
% x0 real
% Initial estimate of a solution
%
% epsilon real, positive
% Required accuracy
%
% Outputs:
%
% X_est real
% Estimate of a solution of (1)
%
% E real, positive
% Estimate of the error in X_est. It is to be expected
% that |X_est-X| <= E, where X is an exact solution of (1).
%
% Note that it is assumed that Matlab implementations exist of the
% function f (in file f.m) and the function f', the derivative
% of f (in the file fdash.m).
%
n = 1;
x(n) = x0;
x(n+1) = x(n) - f(x(n))/fdash(x(n))
while abs(x(n+1)-x(n)) > epsilon
n = n+1;
x(n+1) = x(n) - f(x(n))/fdash(x(n))
end
X_est = x(n+1);
E = abs(x(n+1)-x(n));

Basically this is the closest we have got but we get the error:

??? function [X_est,E] = newton(x0,epsilon)
|
Error: Function definitions are not permitted at the prompt or in scripts.
 
Back
Top Bottom