필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Attempting to have a series for sin stop summing when the precision is correct

조회 수: 1 (최근 30일)
Steven Manz
Steven Manz 2019년 10월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
p = 0;
Pn = sinser(p,x);
figure(3)
plot(x,sin(x),'-k')
hold on
plot(x,Pn)
axis([0 100 -2 2])
title('Sine Curve Series')
xlabel('x')
ylabel('y')
legend('Sin(x)','P')
hold off
function [p,x] = sinser(p,x)
ssx = 1000000*p;
sinx = 1000000*sin(x);
floor1 = floor(ssx);
floor2 = floor(sinx);
if floor1 ~= floor2
b = 1;
else
b = 0;
end
k = 0;
while b == 1
p = p + (((-1)^k*(x.^(2*k+1)))/(factorial(2*k+1)));
k = k + 1;
end
end
For some reason, MatLab is stating that Pn = 0. I am not sure why. I am hoping for Pn to equal some sort of summation that follows the sine curve. Ultimately it should be a 1x1001 matrix, but it is giving me a scalar value. Any help is appreciated.

답변 (1개)

James Tursa
James Tursa 2019년 10월 17일
Inside the sinser function, you never set p to a vector ... it is always just a scalar. If you want to return all of those intermediate values of p, then you need to redo your logic to save them in a vector (e.g., using p(k) and p(k+1) etc inside the function as appropriate).

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by