Evaluating a function in matlab
I have expanded sin function into a Taylor series. Now I want to evaluate it at specific point开发者_Go百科. I get strange "MuPAD" errors in matlab. What am I doing wrong?
function r1=taylor_sine
syms x;
mysine = taylor(sin(x), 63, 0);
r1 = funm(220, mysine);
Did you really mean 220? Or did you mean 220 degrees in which case you should use 220*pi/180?
I think it should be subs not funm
r1 = double( subs(mysine, x, 220*pi/180) );
I'm not 100% familiar with the syntax you're using, maybe the inline function syntax is slightly different from the expanded syntax, however it looks like your function doesn't have a cleary defined input and output. A non-inline matlab function should look like this:
%Comment
function [ theta ] = FunctionName( alpha, beta )
theta = alpha + beta;
end
Try to create your function in a separate .m file (filename the same as the function name). After you've created the .m file make sure that it's located in the search path of MatLab (check if the autocompleter shows your function name when you type it in partially).
As for the actual body of your function, I see a few weird things. What is "syms x" supposed to do? I would replace this line with "x = -pi:0.001:pi;" (have x be a vector from -pi to pi with increments of 0.001). or something analogue to that.
Also for normal Taylor aproximation I would use taylor(sin(x), 63) (the overload with 'v'- does a Maclaurin approximation). Also I wouldn't do a Taylor approximation up to the 63-1th order, that's way too high, maybe MatLab crashes on that.
In the following picture you can see that the 7th order approximation is already extremely good between -pi and pi.
精彩评论