Writing a sin function program dealing with factorials, help in editing and error?

I am trying to write a specific program involving the sine function and factorials.
The problem states that:
sin(x) = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + x^9 / 9!
I am trying to get the error rate very low, basically having the number of terms it takes to get enough accuracy by 10^-5, so that:
abs[sin(x) - sk(x)] / abs[sin(x)] = 10^-5
I know the sk equation should be written as:
sk = x-x^3 / 3! + .......x^(2k-1) / (2k-1)!
I am starting out my program with:
for s = 1: maxs
nsteps(s) = (ones(s)
end
I am confused on how to add on to my code to create my desired result, I know intuitively that when plotting this, the #'s should increase as x increases.
[Merged information from duplicate question]
I am trying to write a specific program involving the sine function and factorials.
The problem states that:
sin(x) = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + x^9 / 9!
I am trying to get the error rate very low, basically having the number of terms it takes to get enough accuracy by 10^-5, so that:
abs[sin(x) - sk(x)] / abs[sin(x)] = 10^-5
I know the sk equation should be written as:
sk = x-x^3 / 3! + .......x^(2k-1) / (2k-1)!
I am starting out my program with:
for s = 1: maxs
nsteps(s) = (ones(s)
end
I am confused on how to add on to my code to create my desired result, I know intuitively that when plotting this, the #'s should increase as x increases.
Here is my code:
function [s] = fact(t)
s = 1:max(s);
for s = 1:max(s)
nsteps(s) = ones(s);
end
for t = (1:2:2*k-1);
(-1)^(0:k-1) .* x^t ./ factorial(t);
end
I receive this error when performing it:
??? Undefined function or variable 's'.
??? Undefined function or variable "s".
Error in ==> fact at 2
s = 1:max(s);
HELP PLEASE!

Réponses (2)

t = (1:2:2*k-1);
(-1).^(0:k-1) .* x.^t ./ factorial(t)
Or something like that.
(Note: corrected -- ^ operators had to be .^ )

7 commentaires

here's my code:
function sin(t) = fact(t)
for t = (1:2:2*k-1);
(-1)^(0:k-1) .* x^t ./ factorial(t);
end
And here is the error I receive:
??? Error: File: fact.m Line: 1 Column: 17
The expression to the left of the equals sign is not a valid target for an
assignment.
?? it seems valid to me
function s = mysin(t)
s = 1:max(s);
for s = 1:max(s)
nsteps(s) = ones(s);
end
for t = (1:2:2*k-1);
(-1)^(0:k-1) .* x^t ./ factorial(t);
end
??? Undefined function or variable "s".
Error in ==> mysin at 2
s = 1:max(s);
still undefined?
You have to define s before you use it.
You will also have to define k before you use that.
Your nsteps variable will fail if s somehow becomes greater than 1, as ones(2) would be a 2 x 2 array and that is not going to fit within the single numeric array location nsteps(2)
I do not know what you are trying to do with your for loop, or your nsteps variable.
Hint: (-1).^(0:k-1) .* x.^t ./ factorial(t) is an expression, not a command. You should be doing something with the expression, not just discarding the value of it (which is what a semi-colon means)
I rewrote my code trying to define variables as is:
function s = mysin(t)
s = 0;
for s = 1:max(s)
nsteps(s) = ones(s);
k = 0:.1:pi/2;
x = 0:.1:2;
t = (1:2:2*k-1);
end
while t = (-1)^(0:k-1) .* x^t ./ factorial(t)
end
still receiving this error though:
??? Error: File: mysin.m Line: 9 Column: 9
The expression to the left of the equals sign is not a valid target for an
assignment.
"=" is not permitted in a "while". "while" has to be given a logical test.
Warning: You initialize s to 0, so max(s) is 0, and 1:0 is the empty vector, so your "for" loop will not do any iterations.
What is the purpose of your nsteps vector? Why are you trying to initialize it to a square matrix such as
1 1 1
1 1 1
1 1 1
(i.e., ones(3))

Connectez-vous pour commenter.

I take it this is a programming exercise, not a straight mathematics question - you can work out the number of terms pretty easily using Taylor's theorem with remainder ...

Catégories

En savoir plus sur Exponents and Logarithms dans Centre d'aide et File Exchange

Question posée :

le 23 Avr 2012

Community Treasure Hunt

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

Start Hunting!

Translated by