cos(x) The value of can be represented by the following series.
--> cos(x) = 1 - 1-x^2/2!+x^4/4!-x^6/6! + . . . .
1. Write a mycos function that uses the above series to obtain the value of cos(x).
2. For the difference between the value of cos(2) and mycos(2) provided in Matlab to be 0.001 or less,
Write a code to determine the minimum number of terms of the critical series.
3. Configure the maximum number of iterations to be less than 10.
I'd like to know the matlab code for this problem. Please help me.
My English may be poor and my grammar may be wrong.
function cos(x) = mycos(x,n)

댓글 수: 12

James Tursa
James Tursa 2021년 5월 12일
편집: James Tursa 2021년 5월 12일
HInt: Given a value of x, write a loop to sum up those terms. Since this is an alternating series with monotonically decreasing magnitude terms for x=2, you can quit adding terms when the next term is less than the desired tolerance. Do you know how to write a loop and limit the number of iterations to less than 10?
Jan
Jan 2021년 5월 12일
Please post what you have tried so far.
justlikethat
justlikethat 2021년 5월 14일
편집: justlikethat 2021년 5월 14일
I don't know how to create loops that limit the number of iterations to less than 10
I'm a very newbie and I don't know how to start.
justlikethat
justlikethat 2021년 5월 14일
@Jan 처음 코드를 어떻게 시작하면 좋을까요?
for k=1:9
%your code
end
justlikethat
justlikethat 2021년 5월 14일
@David Hill What does "k" mean?
David Hill
David Hill 2021년 5월 14일
Sounds like you should go through the MATLAB onramp.
justlikethat
justlikethat 2021년 5월 14일
less than 10 --> k = 0:10 (k <= 10)
Isn't it this? not k = 1:9
승현 표
승현 표 2022년 3월 30일
저 혹시... 어떤 책을 쓰셨는지 알 수 있을까요
Ayoub
Ayoub 2022년 12월 7일

Help me please

Ayoub
Ayoub 2022년 12월 7일
Cos(x)=sum (-1)^(k*2^2k)/(2k!) Sum[k n] K=0
Jan
Jan 2022년 12월 8일
@Ayoub: Please open a new thread for a new question. It is not clear, what you are asking for or how we can help you.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2021년 5월 14일
편집: Jan 2022년 12월 8일

0 개 추천

Please read the getting started chapters of the documentation and see Matlab's Onramp tutorial.
Then split the question into parts and solve them one by one.
  1. "Write a mycos function"
function y = mycos(x)
end
2. "above series to obtain the value of cos(x)"
function y = mycos(x)
y = 1 - x^2 / factorial(2) + x^4 / factorial(4) - x^6 / factorial(6);
end
This should be expanded in a loop:
function y = mycos(x)
y = 0;
for k = 0:10
y = y + (-1)^k * x^(2*k) / factiorial(2*k);
end
end
But why stop at k==10 oder anyother specific value?
"value of cos(2) and mycos(2) provided in Matlab to be 0.001 or less"
function [y, k] = mycos_2()
realY = cos(2);
y = 0;
k = 0;
while abs(y - realY) > 0.001
y = y + (-1)^k * x^(2*k) / factorial(2*k);
k = k + 1;
end
end
"Configure the maximum number of iterations to be less than 10."
function [y, k] = mycos_2()
realY = cos(2);
y = 0;
k = 0;
while abs(y - realY) > 0.001 && k < 10
% ^^^^^^^^^
y = y + (-1)^k * x^(2*k) / factorial(2*k);
k = k + 1;
end
end
Fine. But without reading the documentation and to understand how Matlab works, such a solution is completely useless. Do you see it? This wastes your time only.

댓글 수: 2

justlikethat
justlikethat 2021년 5월 14일
편집: justlikethat 2021년 5월 15일
I'm trying to run this code. I changed mycos_2() to mycos(x)
Is this right?
And I don't understand this part --> [y, k]
why did you write it like this?
lastly, I made ' ( y = y + (-1)^k * x^(2*k) / factiorial(2*k);) ' like this.
--> y = (-1)^(k-1)*x^((k-1)*2/factorial((k-1)*2);
What's the difference?
Jan
Jan 2021년 5월 19일
"[y, k]" is the output of the function. So the caller can know, how many iterations have been needed.
It does not matter, if you run a loop from 0 to n-1 and use k as value, or if the loop goes from 1 to n and k-1 is used. Both methods produce the same numbers.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mahaveer Singh
Mahaveer Singh 2021년 5월 19일
편집: Mahaveer Singh 2021년 5월 19일

0 개 추천

% n is required length of series.Give initial value of n as your imagination to speed up of %calculation.
function y = mycos(x,n)
y = 0;
for i= 0:2:2*n
y = y + ((-1)^(i/2)) *(x^(i)) / factiorial(i);
end
end
while y-cos(x)>0.001
n=n+1;
y=mycos(x,n);
end

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

질문:

2021년 5월 12일

편집:

Jan
2022년 12월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by