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

1 次查看(过去 30 天)
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?
  3 个评论
Walter Roberson
Walter Roberson 2013-11-3
Your professor might want you to structure the loop differently:
while TEST
...
end
can be rewritten as
while true
...
if ~TEST; break; end
end
Leonardo Concepcion
sixwwwwww your code was good, but my prof say that was missing a part, that the program has to stop when E<=0.000001 like it says in the problem.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2013-11-3
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))
  11 个评论
Image Analyst
Image Analyst 2013-11-4
You could get rid of the temporary variable "a", but that's about the only way to simplify it.
Let me ask you, when do you think the while loop should quit ? When E < 0.000001? Well that's what it does. Let me ask in a different way. How long do you think the loop should keep going ? Should it keep going while the error is large ? Larger than 0.000001? Well that's what it does. Can you answer those two questions for me?
I hope someone else contributes here to explain it in a different way because I just don't know what else to say to him to make it clear.
Walter Roberson
Walter Roberson 2013-11-4
编辑:Walter Roberson 2013-11-4
Change
fprintf('The value of the Taylor series after %d terms is %.7f.\nThe true value of sind(%0.1f) is: %0.7f\n',
to
fprintf('The value of the Taylor series after %d terms is %012g.\nThe true value of sind(%0.12g) is: %0.12g\n',

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by