Entering a value for x in a returned equation from a Taylor series.

3 views (last 30 days)
I'm taking a class and have been assigned a problem from our text. I have no problem working it with a pen and paper. I have a big problem working it in MatLab. The problem statement is use zero through third order Taylor series expansion to predict f(3) for f(x)= 25*x^3-6*x^2+7x-88 using a base point at x=1. Compute the true percent relative error for each approximation. The code I have so far does the Taylor expansion fine but I am unable to plug the value of three into the returned equations. How can I do this? My code is
function [g] = ans(y)
x=3
trueval=25*x^3-6*x^2+7*x-88
syms x
f=25*x^3-6*x^2+7*x-88
for n=(0:4)
y=taylor(f,n,1)
end
Any help would be greatly appreciated. Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 30 Jan 2012
It is not a good idea to define x as numeric and then overwrite it with a symbol. There are some cases where that can land you in trouble with the symbolic toolbox, besides it just being difficult to read and debug.
Your routine expects y as an input, but you never use it as an input and instead calculate y as an output.
I suggest
f = @(x) 25*x^3-6*x^2+7*x-88;
sample_at = 3;
trueval = f(sample_at);
syms xx;
fs = f(xx);
for n = 0:4
y = double( subs( taylor(fs, n, 1), xx, sample_at ) );
disp([n, y]);
%now calculate the relative error
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!