Write code in MATLAB that will compute the cosx without usin

Write code (in MATLAB) that will compute the cos(x) without using built in trig functions. Use only these operators/functions (*,^,factorial(),-,+)

I have literally no idea where to start.

Solution

In this case the formula under study is the cosine series, defined as

We obviously can’t use an infinite number of terms, but we can see how accurate is our approach with a few terms. Our proposed function is :

function y = cos1(x)
n = 0 : 100;
y = sum((-1).^n .* x.^(2*n) ./ factorial(2*n));

We are not using for-loops (that’s vectorization), but we’re using some built-in functions such as sum and factorial.

We are assumming that x is a scalar number, and our calculation includes only 100 terms. We’re just implementing the formula above... We must keep in mind that this is an approximation only.

When we test the code by comparing what the actual ‘cos’ function calculates against our result, then

x = 10;
cos(x)
y = cos1(x)

x = 40;
cos(x)
y = cos1(x)

Using Matlab we get....

ans = -0.8391
y =   -0.8391

ans = -0.6669
y = N/A

We can see that our result is good enough for the first case, but it’s not good for the second case. We must take into account that the built-in factorial function uses an algorithm with double precision numbers, and since double precision numbers only have about 15 digits, the answer is only accurate for N <= 21. We must be very aware of the limitations of our resources.

Hope it Helps...

Write code (in MATLAB) that will compute the cos(x) without using built in trig functions. Use only these operators/functions (*,^,factorial(),-,+) I have liter

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site