MATLAB: Need help making a script!!! any help?please

taylor serieswhile loop

This is the problem to make the scrip: http://i.imgur.com/EkSTC0x.png?1
That is the scrip that someone recommended to me but my professor say that was wrong, and that I have to use the student version of matlab to do it, with the basic thing!!
x_deg = input('Input angle in degrees: ');
x = degtorad(x_deg);
sumTerms = 0;
n = 0;
E = Inf;
while E > 1e-6
a = ((-1)^n * x^(2 * n + 1)) / factorial(2 * n + 1);
prevSum = sumTerms;
sumTerms = sumTerms + a;
if n ~= 0
E = abs((sumTerms - prevSum) / prevSum);
end
n = n + 1;
end
fprintf('Value of sin(%0.1f) is: %0.5f\n', x_deg, sumTerms
I have the student version, so something in this program is not letting me run the script, But also my professor say that was wrong and that I have to make the program stop when E<=0.000001?

Best Answer

  • degtorad() is not part of base MATLAB. Use x_deg = x * pi/180 instead. Also, take a look at your duplicate question, which has been answered. Here is the corrected code, along with the improved fprintf() that I gave in your original duplicate question (which you ignored for some reason):
    clc;
    clear all;
    x_deg = input('Input angle in degrees: ');
    x = x_deg*pi/180
    sumTerms = 0;
    n = 0;
    E = Inf;
    while E > 1e-6
    a = ((-1)^n * x^(2 * n + 1)) / factorial(2 * n + 1);
    prevSum = sumTerms;
    sumTerms = sumTerms + a;
    if n ~= 0
    E = abs((sumTerms - prevSum) / prevSum);
    end
    n = n + 1;
    end
    fprintf('The value of the Taylor series after %d terms is %.7f.\nThe true value of sind(%0.1f) is: %0.7f\n', ...
    n, sumTerms, x_deg, sind(x_deg))