Write a script file that will compute the sine of an angle using the Taylor series formula:

조회 수: 21(최근 30일)
John Doe 2021년 9월 29일
댓글: Mathieu NOE 2021년 10월 4일
Write a script file that will compute the sine of an angle using the Taylor series formula:
The program will prompt the user to input the angle in degrees, and the number of terms in the series. Use the program to calculate sin(150 degrees) using 5 and 9 terms.
Note: Can anyone help mw with this? So far this is what I have and I am not sure if it is even correct.
disp("Input the angle in degrees (x) and the number of terms (n)")
x = input('x: ');
n = input('n: ');
sum = 0;
deg = x*180/pi;
for k = 0:n
y = ((((-1)^k)*deg^(2*k+1)))/factorial(2*k+1);
sum = sum + y;
end;
fprintf('sin(%3.2f) = %1.2fln',x,sum)

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE 2021년 9월 29일
hello
please don't use sum or any other native matlab function names for variables names in your code; this can shadow the native function and create trouble in code execution (and unpredictable results).
beside that , your conversion from degrees to radian was wrong (as x is supposed to be in rad in the taylor formula)
also no need for for loop as your code can be easily vectorized.
code updated :
disp("Input the angle in degrees (d) and the number of terms (n)")
d = input('d: ');
n = input('n: ');
x = d*pi/180;
% out = 0;
% for k = 0:n
% y = ((((-1)^k)*x^(2*k+1)))/factorial(2*k+1);
% out = out + y;
% end;
k = 0:n;
y = ((((-1).^k).*x.^(2*k+1)))./factorial(2*k+1);
out = sum(y);
fprintf('sin taylor (%3.2f) = %1.4f\r',d,out) % taylor serie output
fprintf('sin (%3.2f) = %1.4f\r',d,sin(x)) % sin function output
fprintf('error percentage (%3.2f) = %1.4f\r',d,error_percent) % error (%) output
댓글 수: 4표시숨기기 이전 댓글 수: 3
Mathieu NOE 2021년 10월 4일
sorry
probably error of copy paste - that line disappeared by mistake
correction :
disp("Input the angle in degrees (d) and the number of terms (n)")
d = input('d: ');
n = input('n: ');
x = d*pi/180;
% out = 0;
% for k = 0:n
% y = ((((-1)^k)*x^(2*k+1)))/factorial(2*k+1);
% out = out + y;
% end;
k = 0:n;
y = ((((-1).^k).*x.^(2*k+1)))./factorial(2*k+1);
out = sum(y);
error_percent = 100*abs((out-sin(x))./(sin(x)+eps)); % added eps to avoid zero division for case sin(x) = 0
fprintf('sin taylor (%3.2f) = %1.4f\r',d,out) % taylor serie output
fprintf('sin (%3.2f) = %1.4f\r',d,sin(x)) % sin function output
fprintf('error percentage (%3.2f) = %1.4f\r',d,error_percent) % error (%) output

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Chunru 2021년 9월 29일
disp("Input the angle in degrees (x) and the number of terms (n)")
Input the angle in degrees (x) and the number of terms (n)
%x = input('x: ');
x = 45;
%n = input('n: ');
n = 100;
sum = 0;
for k = 0:n
y = (-1)^k * xrad^(2*k+1) /factorial(2*k+1);
sum = sum + y;
end
fprintf('sin(%.2f) = %.6f\n', x, sum)
sin(45.00) = 0.707107
댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Matt J 2021년 9월 29일
Easier:
n=10;
x=pi/7;
k=0:n;
T=sum( ((-1).^k .* x.^(2*k+1))./factorial(2*k+1) )
T = 0.4339
approxError=T-sin(x)
approxError = -5.5511e-17

댓글을 달려면 로그인하십시오.

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by