MATLAB Answers

# Please Help in MATLAB .

30 views (last 30 days)
RORO . on 18 Nov 2011
How I can solve this functions?
(Functions)
A. Write a function Sin() that calculates the sine of the supplied angle (given in degrees) using:
! indicates factorial. 5! = 5 x 4 x 3 x 2 x 1 = 120
Call to the function should be : Result=sin(32.5); means calculate the sine of 32.5 degrees.
B. Write a function cos() that calculates the cosine of the supplied angle (given in degrees) using:
Call to the function should be : Result=cos(32.5); means calculate the cosine of 32.5 degrees
Test your program for A) and B).
Ask the user for the number of terms to use in the series then output all the values from 0 to 180 degrees from your functions.
Also, output the values from the library predefined functions sin( ) and cos( ) for comparison purpose. Use the same unit for comparison.
Check whether sin( ) and cos( ) functions require the angle to be passed in radians or in degrees.
degrees = radians x 180/
radians = degrees x /180
!! IMPORTANT NOTE: First write the first part A) and test it. Then if it works just “cut & paste” and include the second cosine part.
Grading:
Ex#1 Ex#2 EX#3
User prompts 0. 25 0.25 0.25
Comments 1.0 0.75 0.75
Identifier names 0. 25 0.25 0.25
Indentation 0. 25 0.25 0.25
Program code 3.75 2.5 2.5
Output correctness 1.25 1.50 1.50
Output format 1.25 0.5 0.5
Total 8 6 6
##### 8 CommentsShowHide 7 older comments
Image Analyst on 19 Nov 2011
Well I'll give Walter a bonus mark, for whatever that's worth.

Sign in to comment.

### Answers (2)

Jan on 20 Nov 2011
"How I can solve this functions?"
1. Learn the programming fundamentals of Matlab. Reading the Getting Started chapters of the documentation is recommended.
2. Use the details explained in the lessons to answer solve the questions.
3. In case of problems, ask you tutor/professor/teacher.
4. If you have a specific question, ask in this forum.
##### 1 CommentShowHide None
Julián Francisco on 20 Nov 2011
@Jan Simon: You are right.

Sign in to comment.

Julián Francisco on 18 Nov 2011
The trigonometric function sin(x) can be approximated by the infinite series sin(x) = x^1/1! – x^3/3! + x^5/5! – x^7/7! + x^9/9! - … and the trigonometric function cos(x) can be approximated by the infinite series cos(x) = 1 – x^2/2! + x^4/4! - … Since both series are rapidly converging (for whatever value of x), by summing only a small number of terms (with the rest truncated or ignored), one can obtain very good approximation of sin(x) or cos(x) for any given value of x.
This the code for the sine function:
function y = f_sin(x_deg,n)
% x_deg is the argument of the sine function in degrees.
% n is the number of terms used by the series expansion of the sine
% function.
x_rad = degtorad(x_deg); % convert the argument to radians due to the ...
% infinite series uses radians
y = 0;
for j=1:n
y = y + (-1)^(j+1)*(x_rad)^(2*j-1)/factorial(2*j-1);
end
end
This the code for the cosine function:
function y = f_cos(x_deg,n)
% x_deg is the argument of the cosine in degrees
% n is the number of terms used by the series expansion of the sine
% function
x_rad = degtorad(x_deg); % convert the argument to radians due to the ...
% infinite series uses radians
y = 0;
for j=1:n
y = y + (-1)^(j+1)*(x_rad)^(2*j-2)/factorial(2*j-2);
end
end
The following examples show that the above functions work well.
% Compare the f_cos function with the Matlab's built-in cos function
f_cos(2,2),
cosd(2),
% Compare the f_sin function with the Matlab's built-in sin function
f_sin(2,2),
sind(2),
##### 10 CommentsShowHide 9 older comments
Julián Francisco on 20 Nov 2011
@Walter Roberson: Two comments.
1º) The Maclaurin series of a function is the Taylor series of that function about x = 0 (i.e. Maclaurin expansion is only valid for argument's absolute values close to zero).
2º) sin(x*Pi/180) implies that the argument of the sine function, x*Pi/180, is expressed in radians although the value of x is degrees. Indeed, a variable change is being applied to be able to set x in degrees as input argument but the sine function is working in radians. If you do that, then the sine expansion formula (direct degrees version) will be
sin(x*Pi/180) = x*Pi/180 -(x*Pi/180)^3/3! + (x*Pi/180)^5/5! -...
where the argument x is expressed in degrees.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by