6a. Please use matlab
Objectives: Write a convergent algorithm using Taylor
series and use global variables
For Taylor Series. Study the series to see how it converges. Note
that
for these Trig functions, Taylor converges quickly. The number of
terms will vary based on the size of the angle and the
size of epsilon.
For this program you will write a script and two functions, sine
and cosine using Taylor series (NOTE: you can use any
name except the Matlab function names sin and cos for these
functions). Refer to ME1101 Taylor Series DOC. The
purpose of this program is to test your sine and cosine function to
insure it solves for sin and cos accurately.
CONCEPTS:
You are to use three global variables k, csine, and ccosine. k is
to affect epsilon [epsilon (or e if you like) should equal
10^ - k.].
You will use k to improve the accuracy of your functions sine and
cosine compared to Matlab’s sin and cos so that your
function’s results match closely the Matlab sin and cos
functions.
You will use csine as a counter for your sine function and ccosine
as a counter for your cosine function. the variables
csine and ccosine will be used to count how any cycles your sine
and cosine functions require to converge to an accurate
answer. Your results of Sine and Cosine, k, and csine for Sine and
ccosine for Cosine will be recorded in the Excel sheet
TEST TAYLOR.
Requirements:
1. Write a Script that cycles until user stops it.
2. Ask the user to input k a global variable in the script, k will
then be used in both of your 2 functions. Epsilon in
each function will be 10^-k. So, by increasing k you will be
improving the accuracy of your functions.
3. Ask the user for an angle in radians.
4. Using the angle the user entered, call both your sine and cosine
function, as well as the Matlab sin and cos
functions with the same angle. Compare the results for a range of
angles between 0 and π. Pick at least 4
different angles across this range. Adjust your input of k until
you get answers close to the Matlab functions.
5. Write your two functions for sine and cosine using Taylor
series.
(Optional): 6. Fill in the Excel sheet TEST TAYLOR in the Lab
assignment in Blackboard.
SOLUTION- I have solve the problem in Matlab code with comments  for easy understanding :) CODE- Tylor_series.m: warning off; while (1) %read k value from the user in = inputdlg('Enter k angle in degrees: ','Input',[1 40]); %user dialog buttons choice = questdlg('Select the sine, cosine, or sinh', ... 'Dialog buttons','sine','cosine','sinh','stop'); % Handle response k = str2num(in{1}); %convverting fron degree to radian r = k * pi/180; %check the user choice switch choice case 'sine' %call function Tylor_series_sine T = Tylor_series_sine(r); %call Trig sin funcion for comparision s = sin(r); fprintf('Tylor_series_sine = %1.4f ',T) fprintf('and Trig sine = %1.4fn',s); case 'cosine' %call function Tylor_series_sinh T = Tylor_series_cosine(r); %call Trig cos funcion for comparision s = cos(r); fprintf('Tylor series coine = %1.4f ',T) fprintf('and Trig cos = %1.4fn', s); case 'sinh' %call function Tylor_series_sinh T = Tylor_series_sinh(r); %call Trig sinh funcion for comparision s = sinh(r); fprintf('Tylor series sinh = %1.4f ',T); fprintf('and Trig sinh = %1.4fn', s); end %user dialog for Stop choice = questdlg('Do you want to ... See the full answer