Array processing using Taylor Series and FOR Loops to approximate sin value for each element in that array.
Show older comments
I am using x = [-3:0.5:3] as a test value
My code is giving correct sin(x) values for the negative part of this array. However, it gives 0's for when x goes to 0 or greater. The array can be of any type and dimensions.
Taylor Series: 
function [y] = SINM(x)
%SINM This function takes the array x and processes the approximate sin
%value of it.
% The value of sin is approximately calculated using Taylor Series
% from the input array x.
Sum = zeros(size(x));
T = 1E-12; %defining tolerance.
y = zeros(size(x));
[i,j] = size(x);
for n = 0:30
for k = 1:i
for l = 1:j
an(k,l) = ((-1)^n).*((x(k,l).^((2*n)+1))./factorial((2*n)+1));
Sum(k,l) = Sum(k,l) + an(k,l);
if abs(an(k,l)) < T || n==30
break
elseif abs(an(k,l)) > T
disp 'More Iterations are needed to reach the specified tolerance.';
end
end
end
end
y = Sum;
end
This the code I have written so far, but I am confused as to why it gives the output of 0 when x >= 0. ^
How can i fix this code to get sin values for all values in the array?
This is the result I am getting.
%SINM(x)...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%More Iterations are needed to reach the specified tolerance...
%...
%...
%...
%ans =
% Columns 1 through 6
% -0.1411 -0.5985 -0.9093 -0.9975 -0.8415 -0.4794
% Columns 7 through 12
% 0 0 0 0 0 0
% Column 13
% 0
6 Comments
dpb
on 18 Oct 2021
Have you used debugger to see what's happening when things go wrong? That's a very powerful tool to help find logic errors.
I'd make one comment on the code in general --
>> n=0:5:30;F=factorial((2*n)+1);
>> for i=1:numel(F),disp(F(i)),end
1
39916800
5.1091e+19
8.2228e+33
3.3453e+49
1.5511e+66
5.0758e+83
>>
NB: the growth of the factorial term; this is a very numerically unstable way to compute the nth term as it is the ratio of large numbers.
It may work out with relatively small numbers and tolerances, but is likely to cause grief in general.
Humza Khan
on 18 Oct 2021
dpb
on 18 Oct 2021
Did you use the debugger for a value of x that isn't working as you expect?
Don't try to debug the whole thing; just use a single value that doesn't give the right answer to start...
Have you tested what happens if just pass in one value for x (say 0.5) to prove it isn't something about the looping construct and is actually something to do with the value of x itself?
Start and be methodical and look at each and every piece in turn; don't just make changes just to be making changes...learning debugging skills is a part of learning to code; don't feel "picked on" here... :)
Humza Khan
on 18 Oct 2021
John D'Errico
on 18 Oct 2021
What was the purpose of asking this question multiple times?
Humza Khan
on 19 Oct 2021
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!