最后的MATLAB代码故障 [英] MATLAB code glitch at the end

查看:94
本文介绍了最后的MATLAB代码故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.

场景说明:

我已经编写了MATLAB代码以使用部分和S_n(x)=(-1)^ j * x ^(2j + 1)/(2j + 1)从j = 0到n的总和!近似于罪恶x.我已经编写了一个计算机程序,以通过方法

评估此部分和.

LS:计算和总结从最大的术语到最小的术语

SL:以相反的顺序进行计算.

我已经使用我的程序通过LS和SL两种方法针对x = 0.1、1和10来计算S_n(x),并将结果与​​sin x进行了比较.我用过n = 10、100和1000.

问题:我在代码的最后两行遇到问题.它说"NaN"(不是数字),但在上一列中也显示了异常数字.我究竟做错了什么?谁能帮我吗?

这是我的代码:

第一个功能:

function ret = hw1_6_1(x,n)
ret=((-1)^n)*((x^((2*n)+1))/(factorial((2*n)+1)));
end

第二个功能:

function ret = partialsum(x,n,log)
%% Begin code
% j=n
% 1 to n+1
% LS 0 to n
% SL n to 0
%((-1)^n)*((x^((2*n)+1))/(factorial((2*n)+1)))
clear sum
ret = 0;

if log == 1
    for i=0:1:n
    ret= ret + hw1_6_1(x,i) ;
    i=i+1;
    end
elseif log == 0
    for i=n:-1:0
    ret= ret + hw1_6_1(x,i) ;
    i=i+1;
end
end
end

最后,我的主要代码:

%% Hw 1 Problem 6

% Running approximation of sin x
% LS and SL
% LS == log=1, SL == log=0
% x = 0.1, 1, 10
% n = 10,100,1000
clear all
clc
format long %displays more decimal points

%% For x = 0.1

sin1 = sin(0.1);

% LS 
% n = 10, 100, 1000 Generated in array format

LS1 = [partialsum(0.1,10,1);partialsum(0.1,100,1);partialsum(0.1,1000,1)];

% Now SL

SL1 = [partialsum(0.1,10,0);partialsum(0.1,100,0);partialsum(0.1,1000,0)];

%% For x = 1

sin2 = sin(1);

% LS
% n = 10, 100, 1000 Generated in array format

LS2 = [partialsum(1,10,1);partialsum(1,100,1);partialsum(1,1000,1)];

% Now SL

SL2 = [partialsum(1,10,0);partialsum(1,100,0);partialsum(1,1000,0)];

%% For x = 10

sin3 = sin(10);

% LS
% n = 10, 100, 1000 Generated in array format

LS3 = [partialsum(10,10,1);partialsum(10,100,1);partialsum(10,1000,1)];

% Now SL

SL3 = [partialsum(10,10,0);partialsum(10,100,0);partialsum(10,1000,0)];

%% Comparison stage

Sines = [sin1;sin2;sin3]

Approxs = [LS1 SL1 LS2 SL2 LS3 SL3]'  

我的输出:

    Sines =
   0.099833416646828
   0.841470984807897
  -0.544021110889370


Approxs =
   0.099833416646828   0.099833416646828   0.099833416646828
   0.099833416646828   0.099833416646828   0.099833416646828
   0.841470984807897   0.841470984807897   0.841470984807897
   0.841470984807897   0.841470984807897   0.841470984807897
   2.761090925979680  -0.544021110889270                 NaN
   2.761090925979687  -0.544021110889190                 NaN

谢谢.

解决方案

1)sin_taylor(10)== 2.76是因为在 x 较小的情况下,20度多项式逼近值与正弦值不同. /p>

2)序列x ^ n/n!对于| x |> 1,不是单调的,但是在大约n = 6处有最大值;因此,与| x |< = 1且一个正在迅速消失的序列求和的情况相比,会有一些舍入差异.

3)当 n 大时,分母和分母(10 ^ n,n!)都将表示为无穷大. Inf/Inf 被定义为 NaN ,而 NaN +任何== NaN . 这与partial_sum(10,100)不同,在partial_sum(10,100)中,一个项合计为(x/inf == 0).

I'm new here.

Description of scenario:

I have written MATLAB code to use the partial sum S_n(x)=Sum from j=0 to n of (-1)^j*x^(2j+1)/(2j+1)! to approximate sin x. I have written a computer program to evaluate this partial sum by methods

LS: computing and summing the terms from the largest term fi rst to the smallest term last

and

SL: doing the calculation in the reverse order.

I have used my program to calculate S_n(x) for x = 0.1, 1, and 10 by both methods LS and SL, and compared the results against sin x. I have used n = 10, 100, and 1000.

Question: I am running into a problem in the last two lines of my code. It says "NaN" (Not a number), but also shows deviant numbers in the previous column. What am I doing wrong? Can anyone help me?

Here is my code:

The first function:

function ret = hw1_6_1(x,n)
ret=((-1)^n)*((x^((2*n)+1))/(factorial((2*n)+1)));
end

The second function:

function ret = partialsum(x,n,log)
%% Begin code
% j=n
% 1 to n+1
% LS 0 to n
% SL n to 0
%((-1)^n)*((x^((2*n)+1))/(factorial((2*n)+1)))
clear sum
ret = 0;

if log == 1
    for i=0:1:n
    ret= ret + hw1_6_1(x,i) ;
    i=i+1;
    end
elseif log == 0
    for i=n:-1:0
    ret= ret + hw1_6_1(x,i) ;
    i=i+1;
end
end
end

Finally, my main code:

%% Hw 1 Problem 6

% Running approximation of sin x
% LS and SL
% LS == log=1, SL == log=0
% x = 0.1, 1, 10
% n = 10,100,1000
clear all
clc
format long %displays more decimal points

%% For x = 0.1

sin1 = sin(0.1);

% LS 
% n = 10, 100, 1000 Generated in array format

LS1 = [partialsum(0.1,10,1);partialsum(0.1,100,1);partialsum(0.1,1000,1)];

% Now SL

SL1 = [partialsum(0.1,10,0);partialsum(0.1,100,0);partialsum(0.1,1000,0)];

%% For x = 1

sin2 = sin(1);

% LS
% n = 10, 100, 1000 Generated in array format

LS2 = [partialsum(1,10,1);partialsum(1,100,1);partialsum(1,1000,1)];

% Now SL

SL2 = [partialsum(1,10,0);partialsum(1,100,0);partialsum(1,1000,0)];

%% For x = 10

sin3 = sin(10);

% LS
% n = 10, 100, 1000 Generated in array format

LS3 = [partialsum(10,10,1);partialsum(10,100,1);partialsum(10,1000,1)];

% Now SL

SL3 = [partialsum(10,10,0);partialsum(10,100,0);partialsum(10,1000,0)];

%% Comparison stage

Sines = [sin1;sin2;sin3]

Approxs = [LS1 SL1 LS2 SL2 LS3 SL3]'  

My output:

    Sines =
   0.099833416646828
   0.841470984807897
  -0.544021110889370


Approxs =
   0.099833416646828   0.099833416646828   0.099833416646828
   0.099833416646828   0.099833416646828   0.099833416646828
   0.841470984807897   0.841470984807897   0.841470984807897
   0.841470984807897   0.841470984807897   0.841470984807897
   2.761090925979680  -0.544021110889270                 NaN
   2.761090925979687  -0.544021110889190                 NaN

Thanks in advance.

解决方案

1) sin_taylor(10) == 2.76 is because 20th degree polynomial approximation diverges from sine at much smaller value of x.

2) The sequence x^n / n! for |x|>1, is not monotonic, but has a maximum at about n=6; thus there will be some rounding differences compared to cases, where |x|<= 1 and one is summing up a rapidly vanishing sequence.

3) Both the nominator and the denominator (10^n, n!) will be represented as infinities, when n is large. Inf / Inf is defined as NaN, and NaN + anything == NaN. This is different to partial_sum(10, 100), where one sums up terms (x / inf == 0).

这篇关于最后的MATLAB代码故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆