Please Help in MATLAB .

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 Comments

bym
bym on 18 Nov 2011
what have you done so far and where are you stuck?
RORO .
RORO . on 18 Nov 2011
I did not underdtand this question from my assignment and Iam new in matlab .
RORO .
RORO . on 18 Nov 2011
also, I did not have alot of knowledge to answer this question.
There is no simple program that can solve that assignment, as it has conflicting requirements (function name conflicts, calling sequence does not allow number of terms to be passed but number of terms is required for the second bullet point of B)
It is difficult for us to be of assistance in helping you program according to a particular formula when you have not included the formula.
RORO .
RORO . on 18 Nov 2011
Mr.Roberson could you please answer this question for me even if I did not take all the formula at school. I want to get a bonus mark from this question please.
We can tell by reading the question that right after the note about the meaning of the factorial operator, that there should be a mathematical formula that the instructor expects you to use to calculate sine in degrees, and that there should be a different formula shown after the 'using:' in (B). When you pasted the question in, those formula did not make it; we do not know what you are supposed to use. We could research and find *some* formula to do what is needed, but if it did not match the formula the assignment sets out...
You will need to type in the formulas, somehow, or post the link to an image of the formulas. See http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers for a partial list of places where you can post images.
Jan
Jan on 18 Nov 2011
@Poro: Do I miss something? *Walter* should answer this question and *you* get a bonus mark? When I was studying, it was usual to solve the homework by ourselves. We did not get bonus marks for cheating, but a removal from the register of students.
I do not think that Tony Nicol will be satisfied by your strategy.
Well I'll give Walter a bonus mark, for whatever that's worth.

Sign in to comment.

Answers (2)

Jan
Jan on 20 Nov 2011

1 vote

"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.
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 Comments

The calling sequence is not permitted to include the number of terms. The instructions specifically say Result=sin(32.5) which has no possibility of passing the number of terms.
@Walter Roberson: You are right but one of the points of the section B says: "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". The unique form to relate the sin or cos function with factorials is through a series expansion (also known as infinite series) so there are two possibilities:
- using always the same number of terms for each series.
- using an input argument for it.
"Your functions" has to refer to the functions described in (A) and (B), which are not allowed to take any argument other than the degrees. But the number of terms must be controllable by the user for the purpose of the 0-180 table. Either the instructions are contradictory or the programmer is expected to use a technique other than a calling parameter to get the number of terms in to the function. This is not impossible, but it is above the grade level of the assignment.
There are two important questions with respect to the above sin() and cos() formula:
(1) Are they the *right* formula for the assignment?
(2) If they are, how many terms do they need to be carried to for reasonable accuracy?
With regard to the correctness of the formula: I have calculated the series formula for sine when the value is expressed in degrees, and I have examined the floating point behavior of that formula as compared to Julian's formula (that converts to radians first and takes the sine of that result). My conclusion is that when one uses standard IEEE double precision calculations for the terms, that Julian's convert-first formula will differ noticeably from the direct expansion calculation, for at least some values that are within the range required to be tested.
My examination also shows that in that range, for some values, that calculating the direct series in double precision to *any* number of terms will never converge to the sine value, maintaining an error of approximately 100 * eps. I have not yet found conditions under which the indirect series will not produce an acceptable output if allowed to proceed for sufficient terms.
Thus, it is important that RORO convey to us the actual formula required by the instructor, as the different possible formula are enough to make a provable computational difference.
If you use enough terms, the radians version will match sin() to within eps even up to 180 degrees, but the direct degrees version will never reach that accuracy: the terms will inherently have floating point roundoff (e.g., denominators greater than 2^53) before that accuracy can be reached.
@Walter Roberson: The sine and cosine series expansion are only valid if the argument's value, x, is expressed in radians. In this way, they will converge for whatever value of x in radians.
*That* sine and cosine series expansion, yes. But there are sine and cosine series expansions that calculate directly in degrees. As RORO has not posted the series RORO is expected to use, we don't know which version RORO is required to work with.
@Walter Roberson: The unique series expansions of the sine or cosine functions that I know are the Taylor series (I have searched for different ones on Internet and I have found nothing). If you want to use the direct degrees version with the formulae I have given, it is necessary to make a variable change (from degrees to radians) as I did (note that if you use x=30 instead of x=π/6, then the series expansion will converge to the value of sine of 30 radians instead of to the value of sine of 30 degrees).
maclaurin expansion of sin(x*Pi/180)
@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.

Categories

Tags

Asked:

on 18 Nov 2011

Community Treasure Hunt

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

Start Hunting!