필터 지우기
필터 지우기

sine approximation using a loop

조회 수: 2 (최근 30일)
Yogesh Bhambhwani
Yogesh Bhambhwani 2020년 9월 30일
편집: Walter Roberson 2020년 10월 1일
  댓글 수: 2
James Tursa
James Tursa 2020년 9월 30일
Please post your code here and ask specific questions about it.
Yogesh Bhambhwani
Yogesh Bhambhwani 2020년 9월 30일
편집: Walter Roberson 2020년 10월 1일
%% Problem 3.2 Template
%{
test cases:
x = 0.2; N = 7; -> s = 0.198669330795061
x = 1.85; N = 4; -> s = 0.960597005280723
%}
function [s] = prob3_2(x,N)
% function [s] = prob3_2(x,N)
% takes a scalar angle measure x (in radians) and estimates sin(x) using
% the first N terms of an alternating series
format long
%edit below this line ---------------------
x = pi/2;
error = 1;
n = 1;
count = 0;
while error >= 1*(10^-3);
count = count+1;
terms(count) = (-1)^(count+1)*(x^n)/factorial(n);
s = sum(terms);
n=n+2;
error = abs((sin(x)-s)/sin(x))*100;
end
disp(s)
%edit above this line ----------------------
return

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 1일
편집: Walter Roberson 2020년 10월 1일
x = pi/2;
That is overriding the user input about the value to find the sine of.
function [s] = prob3_2(x,N)
You never use the user input N
n = 1;
you have a variable named n which is confusing considering the input named N
while error >= 1*(10^-3);
You are proceeding until your error is in a certain range, but the problem requirement says
% takes a scalar angle measure x (in radians) and estimates sin(x) using
% the first N terms of an alternating series
so you are not intended to go until error is a particular range: you are to go until you have used a particular number of terms.
error = abs((sin(x)-s)/sin(x))*100;
Is error a percentage, or is an absolute value?
What about the cases where sin(x) = 0 ? You would be dividing by 0, which is not going to give you a useful error measure.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by