Array processing using Taylor Series and FOR Loops to approximate sin value for each element in that array.

조회 수: 7 (최근 30일)
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
Humza Khan
Humza Khan 2021년 10월 19일
Sorry about that! I asked my TA about it who explained the loop structure for 2-D arrays. His explanation made sense so i deleted my query the first time around. But when I started to do it again, I was stuck again so I posted it again for some external guidance.

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

채택된 답변

Matt J
Matt J 2021년 10월 18일
편집: dpb 2021년 10월 18일
The array can be of any type and dimensions.
If so, why does your code assume it will be 2D? Can't it be 3D or 4D? In any case, consider the following modification (which will work for any dimension).
SINM(-3:0.5:3)
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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 = 1×13
-0.1411 -0.5985 -0.9093 -0.9975 -0.8415 -0.4794 0 0.4794 0.8415 0.9975 0.9093 0.5985 0.1411
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.
y = zeros(size(x));
T = 1E-12; %defining tolerance.
for i=1:numel(x)
for n = 0:30
an = ((-1)^n).*((x(i).^((2*n)+1))./factorial((2*n)+1));
y(i) = y(i)+an;
if abs(an) < T || n==30
break
elseif abs(an) > T
disp 'More Iterations are needed to reach the specified tolerance.';
end
end
end
end
  댓글 수: 4
Humza Khan
Humza Khan 2021년 10월 19일
편집: Humza Khan 2021년 10월 19일
Yes, The thing was that i was not sure if I was allowed to use the numel() function for the assignment. But I got confirmation of its usage so it all makes sense to me as using numel() makes the entire process much more efficient and flexible so that it could process arrays of all dimensions.
dpb
dpb 2021년 10월 19일
That's something newbies generally don't catch on to right off the bat...you're well ahead of the game it seems! :)

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by