필터 지우기
필터 지우기

If...end statement for taylor series expansion code

조회 수: 2 (최근 30일)
Zelda Luxenburry
Zelda Luxenburry 2012년 2월 13일
편집: dpb 2013년 10월 9일
this is my function m file, but I cant seem to get it to work. the code runs but the value of mycos1(.5,100) is not equal to cos(.5), for instance. cant seem to find where the mistake is...
function [cosx] = mycos1(x,n)
%evaluates and sums the first n terms of the Taylor series
% use the for loop control structure to express the infinite taylor
% series
if n<=0
error('n must be positive')
end
if rem(n,1)~=0
error('n must be an integer')
end
cosx=1;
i=0;
for i=1:n+1
cosx=cosx+(((-1)^i + x^(2*i))/factorial(2*i));
end
end

답변 (1개)

John D'Errico
John D'Errico 2012년 2월 13일
I'm just wondering why you put a return statement as the very first executable line of code? Is it possible this is the problem? What happens when matlab hits that return?
  댓글 수: 1
Zelda Luxenburry
Zelda Luxenburry 2012년 2월 13일
when I delete the "return", the error reads "line 7 error, not enough input arguments"...what would be a better way to say "if n is nonpositive or not an integer, display error" ?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by