Question
Transcribed Text
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
Programming fundamentalsProblem 1
%The format to use
format long
%The number of terms to use
Number_of_terms=[10 15 25];
%The angle to use
x=5.0; %radians
%First display the answer using the Matlab function sin:
%display_string: the string which we use to display the answer
display_string=['The value of sin(' num2str(x) '):' num2str(sin(x),15)];
%display it
disp(display_string);
%It should display: The value of sin(5):-0.958924274663138
for ind=1:length(Number_of_terms)
%the approximation is as a series, initialize the first term to be zero
%and further add to it.
approximate_sin_value=0;
%The number of terms at this time
N=Number_of_terms(ind);
%Construct the values in the series and add to the
%approximate_sin_value
for s=1:N
to_add=(-1)^(s-1)/factorial(2*s-1)*x^(2*s-1);
approximate_sin_value=approximate_sin_value+to_add;
end
%At the end of this loop all the terms have been added.
%Diplay the result:
display_string=['The approximate value with ' num2str(N) ' terms: ‘ num2str(approximate_sin_value,15)];
disp(display_string);
end
%Output from the program should be:
% The value of sin(5):-0.958924274663138...