Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Leonardo Concepcion
Leonardo Concepcion am 3 Nov. 2013
Bearbeitet: Walter Roberson am 4 Nov. 2013
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 Kommentare
Walter Roberson
Walter Roberson am 3 Nov. 2013
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
Leonardo Concepcion am 3 Nov. 2013
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Nov. 2013
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 Kommentare
Image Analyst
Image Analyst am 4 Nov. 2013
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 am 4 Nov. 2013
Bearbeitet: Walter Roberson am 4 Nov. 2013
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',

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by