Hi
I want to create a table
column 1 will be the value of n from 1 to 20 n= 1, 2, 3, ... , 20
column 2 will be the nth (respectively) taylor approximation of a function [ so n terms of a taylor approximation of ]
column 3 will be the error of this taylor approximation compared to the real value of erf(2) so [erf(2)-nth term taylor approx]
column 4 will be a different series approximation of the same function erf(2) [asymptotic series ] where the double !! means only odd numbers, i.e. .
column 5 will be the error of this second approximation, i.e. [erf(2) minus my n-term asymptotic approximation]
Below is my try. my tayterm doesn't work, neither does my asymterm. The values given are incorrect.
:( thank you.
i would also like gridlines on the table...not sure how to do that.
format long
syms k x t X
%tayt =[taylor(erf(x),x,'Order',2),taylor(erf(x),x,'Order',3),taylor(erf(x),x,'Order',4),taylor(erf(x),x,'Order',5),taylor(erf(x),x,'Order',6),taylor(erf(x),x,'Order',7),taylor(erf(x),x,'Order',8),taylor(erf(x),x,'Order',9),taylor(erf(x),x,'Order',10),taylor(erf(x),x,'Order',11),taylor(erf(x),x,'Order',12),taylor(erf(x),x,'Order',13),taylor(erf(x),x,'Order',14),taylor(erf(x),x,'Order',15),taylor(erf(x),x,'Order',16),taylor(erf(x),x,'Order',17),taylor(erf(x),x,'Order',18),taylor(erf(x),x,'Order',19)];
for n=1:18
N(n)=n;
tayterm(n)=taylor(erf(x),x,'Order',n);
ercol2(n) = erf(2)-tayterm(n);
asymterm(n)=1-2/sqrt(pi)*exp(-2^2)*(symsum((-1)^k*x^(2*k+1)/((2*k+1)*factorial(k)),k,0,n-1));
ercol4(n) = erf(2)-asymterm(n);
end
format long
T = table(transpose(N),transpose(double(tayterm)),transpose(ercol2), transpose(asymterm),transpose(ercol4));
T.Properties.VariableNames = {'n' 'n-term Taylor Approx for erf(2)' 'Error in col 2' 'n-term asymptotic approx of erf(2)' 'Error in col 4'}

7 个评论

In what way does it not work?
I haven't used symbolic math much, but my first thought is that it doesn't want to create an array. Try cell arrays instead:
tayterm{n}=taylor(2/sqrt(pi)*erf(x),t,0,'Order',n);
it gives me incorrect values for the tayterm and asymterm
the curly brackets give me an error
Error using sym/subsasgn (line 942)
Invalid indexing assignment.
Error in testing (line 7)
tayterm{n}=taylor(2/sqrt(pi)*erf(x),t,0,'Order',n);
Could be that your function ( erf(x) ) and variable (t) are different, try:
tayterm(n)=taylor(erf(t),t,0,'Order',n);
but the input x has to do with the bounds of the error function which is an integral of t.
You are mixing up "x, the input to the function" and "x, the value where you evaluate the function". There are a few ways you could fix your code, but
taylor(erf(x),t)
doesn't make sense. It's (almost exactly) like asking for d[f(x)]/dy instead of d[f(x)]/dx.
syms k x t
x0=2;
for n=1:20
N(n)=n;
% Taylor series of erf(x) with respect to x, centered at 0, order n
tayterm(n)=taylor(erf(x),x,0,'Order',n);
% error between taylor apprx and value, at chosen x0
ercol2(n) = double(erf(x0)-tayterm(n));
% issue with asymterm was replacing the first of the x's with 2
asymterm(n)=1-2/sqrt(pi)*exp(-x^2)*(symsum((-1)^k*x^(2*k+1)/((2*k+1)*factorial(k)),k,0,n-1));
ercol4(n) = erf(x0)-asymterm(n);
end
Okay, this is giving me the following error:
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 700)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in testing (line 19)
T = table(transpose(N),transpose(double(tayterm)),transpose(ercol2), transpose(asymterm),transpose(ercol4));
You are trying to turn a symbolic expression into a number. That doesn't work. You can either leave the symbolic expression alone or evaluate it for a particular input (using subs), depending on what you want in the table

请先登录,再进行评论。

 采纳的回答

syms X
tayterm(n) = taylor(erf(X),X,x,'Order',n)
You always need to taylor() a function, not a constant.

5 个评论

thanks for your comment, again, this is the wrong function, i want a taylor function with respect to t not X and it should be around 0 not around x
so the taylor approximation of erf(2) with 2 terms is something like
a taylor approximation of erf(2) in general is something like
the asymptotic approximtion of erf(2) with 2 terms is something like
how can i set a string with the taylor values and then later evaluate it at x=2?
where? in the table? inside transpose? in the loop?
syms t
tayterm(n) = subs( taylor(erf(t),t,0,'Order',n), t, x );
That would taylor around 0 but evaluate at 2

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by