How do I make this function check error tolerance and the max number of iterations?

1 回表示 (過去 30 日間)
Tony
Tony 2016 年 9 月 7 日
コメント済み: Walter Roberson 2016 年 9 月 8 日
This is the code I have so far
function [y,N] = sine_taylorN(x,es)
% This function evaluates the Taylor's series (Maclaurin's
% series) of the sine function evaluated at x and determines the number of
% terms required for convergence based on relative error.
% Inputs/Output:
% x = scalar value to be evaluated at
% es = stopping criterion requited for relative error in %
% N = number of terms required in the series for convergence
% to relative error accuracy of es
% y = approximate value of sine(x) by Maclaurin's series
y = 0; er = 100; k = 0; % Initializations
yexact = sin(x);
while er >= es
k = k +1;
y = y + (-1)^(k+1)*x^(2*k-1)/factorial(2*k-1);
er = abs((yexact-y)/yexact)*100; % relative error in percentage
end
N = k;
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 8 日
First you need to define what the maximum number of iterations is, and what the behavior is to be if the maximum number is exceeded.
Second you need to read about the && operator

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by