How to write a prorgram to compute sin(x) without using trigonometric function that uses taylor series?

6 views (last 30 days)
For a homework assignment I am asked the following, I am asking for help here because my Prof. will not help and the TA's do not know how to do it. Here it is:
Write a program to compute sin(x). The name of the program is dsin, and it accepts the user input – x – in degrees. Your program converts x to radians, and uses taylor series to compute sin(x) within some defined accuracy. The program has two versions; one version uses the ‘while’ construct, and the other version uses the ‘for’ construct.
Note: • Taylor series approximates the sinusoidal function for -180 ≤ x ≤ 180 degrees (or -pi ≤ x ≤ pi). If your input is out of this range, your program must translate it to a value within the range. • When you run dsin, it prompts; Enter angle in degrees: • After you type desired angle, the result will return. • You are not allowed to use the trigonometric functions provided by Matlab!

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Feb 2017
Jake - from https://en.wikipedia.org/wiki/Taylor_series, a Taylor series is a representation of a function as an infinite sum of terms that are calculated from the values of the function's derivatives at a single point... If the Taylor series is centered at zero, then that series is also called a Maclaurin series. So you will be implementing the Maclaurin series for sin(x). Since the series is a representation of a function as an infinite sum of terms, then you will use while or for loop to compute the sum (to some defined accuracy).
As this is a homework question, we can only give out hints so you will need to post a describe what you have attempted. Are you observing any errors and, if so, what are they?

More Answers (1)

John BG
John BG on 12 Feb 2017
Edited: John BG on 12 Feb 2017
Hi Stringfellow
1.
Many hand held calculators use the CORDIC algorithm
.
that is pretty good at approximating sin and cos trigonometric functions.
MATLAB includes a group of CORDIC related functions, following MATLAB example from this link:
.
% Create 1024 points between [0, 2*pi)
stepSize = pi/512;
thRadDbl = 0:stepSize:(2*pi - stepSize);
thRadFxp = sfi(thRadDbl, 12); % signed, 12-bit fixed-point
cosThRef = cos(double(thRadFxp)); % reference results
% Use 12-bit quantized inputs and vary the number
% of iterations from 2 to 10.
% Compare the fixed-point CORDIC results to the
% double-precision trig function results.
for niters = 2:2:10
cdcCosTh = cordiccos(thRadFxp, niters);
errCdcRef = cosThRef - double(cdcCosTh);
end
figure
hold on
axis([0 2*pi -1.25 1.25]);
plot(thRadFxp, cosThRef, 'b');
plot(thRadFxp, cdcCosTh, 'g');
plot(thRadFxp, errCdcRef, 'r');
ylabel('cos(\Theta)');
gca.XTick = 0:pi/2:2*pi;
gca.XTickLabel = {'0','pi/2','pi','3*pi/2','2*pi'};
gca.YTick = -1:0.5:1;
gca.YTickLabel = {'-1.0','-0.5','0','0.5','1.0'};
ref_str = 'Reference: cos(double(\Theta))';
cdc_str = sprintf('12-bit CORDIC cosine; N = %d', niters);
err_str = sprintf('Error (max = %f)', max(abs(errCdcRef)));
legend(ref_str, cdc_str, err_str);
.
.
CORDIC requires a table with atan values
2.
Another approximation, between 0º and 180º, for sin function, is Bhaskara I's
dx=180/210;
x=[0:dx:180-dx];
y=sind(x);
y2=4*x.*(180-x)./(40500-x.*(180-x));
err=abs(y-y2);
max(err)
=
0.001630316128806
or from same author, approximate sin between 0º and 180º with 2 parabolas:
y_upper=x.*(180-x)./8100;
y_lower=x.*(180-x)./9000;
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  5 Comments
John D'Errico
John D'Errico on 26 Feb 2017
John BG - As seems to be a common event for you, you have spent a great deal of time doing a homework assignment for someone, something that we explicitly try NOT to do here. Worse, you have never bothered to actually read the requirements.
Jan
Jan on 26 Feb 2017
@John BG: As I have written already, your remarks are interesting, but they do not match the asked question. You have missunderstood the title, but the body of the question is unequivocal.
You had selected your answer as accepted by your own. This was a bad choice and this thread is a perfect example, why selecting own answers should and must be forbidden in this forum.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!