MATLAB - Taylor Series
I have a question that asks me to compute the taylor series of cos(x) in Matlab with a function, like this:
function [cosx] = my_cos1(x,N)
Where x is the number we want to compute and N is the number of terms.
I have completed this question and now need to incorporate an optional 3rd parameter, 'eps' which is the accuracy. Where basically if the absolute value of one of the terms of the taylor series is less than the given accuracy, you don't add it on.
So I have everything to do with how to add an optional parameter at the top, but don't know how to write the code for the eps part. So this is what I have so far:
function tayexp=taylorcos(varargin)
K=length(varargin);
if K>3
disp('Too many parameters!')
else
if K<2
disp('Not enough of parameters')
else
x=varargin{1};
N=varargin{2};
if K==2
tayexp=my_cos1(x,N);
else
eps=varargin{3};
format long
r = 0;
for i = 0:N-1
t=((((-1)^i) * (x^(2*i)))/factorial(2*i));
if abs(t)>eps
r=r+t;
end
end
tayexp=r;
end
end
end
end
Any help greatly appreciated!
function [cosx] = my_cos1(x,N)
Where x is the number we want to compute and N is the number of terms.
I have completed this question and now need to incorporate an optional 3rd parameter, 'eps' which is the accuracy. Where basically if the absolute value of one of the terms of the taylor series is less than the given accuracy, you don't add it on.
So I have everything to do with how to add an optional parameter at the top, but don't know how to write the code for the eps part. So this is what I have so far:
function tayexp=taylorcos(varargin)
K=length(varargin);
if K>3
disp('Too many parameters!')
else
if K<2
disp('Not enough of parameters')
else
x=varargin{1};
N=varargin{2};
if K==2
tayexp=my_cos1(x,N);
else
eps=varargin{3};
format long
r = 0;
for i = 0:N-1
t=((((-1)^i) * (x^(2*i)))/factorial(2*i));
if abs(t)>eps
r=r+t;
end
end
tayexp=r;
end
end
end
end
Any help greatly appreciated!
0
Comments
if abs(t)>eps
r=r+t;
This seems like you already accomplish what you are asking about. You don't do anything when abs(t) < eps. What am I missing? I don't know MATLAB, but I do know algorithm and coding design, so maybe I can help tease out the answer.
tayexp=mycos1(x,N);
doesn't take in the .01 value. Is that correct?
Can you provide the matlab code in a tabbed format (so I can follow it better)? For example, wrap your tabbed code with < code> and </ code> (remove the space before code in both)
function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2<br /> disp('Not enough of parameters') else x=varargin{1}; N=varargin{2};
The ending also confused me, because you create a function (tayexp) then set it to something else that is not defined (my_cos1()) and then set it again to r.
function tayexp=taylorcos(varargin)
K=length(varargin);
if K>3
disp('Too many parameters!')
else
if K<2</code>
disp('Not enough of parameters')
else
x=varargin{1};
N=varargin{2};
if K==2
tayexp=my_cos1(x,N);
else
eps=varargin{3};
format long
r = 0;
for i = 0:N-1
t=((((-1)^i) * (x^(2*i)))/factorial(2*i));
if abs(t)>eps
r=r+t;
end
end
tayexp=r;
end
end
end
end
function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2 disp('Not enough of parameters') else x=varargin{1}; N=varargin{2}; if K==2 tayexp=my_cos1(x,N); else eps=varargin{3}; format long r = 0; for i = 0:N-1 t=((((-1)^i) * (x^(2*i)))/factorial(2*i)); if abs(t)>eps r=r+t; end end tayexp=r; end end end end
Can you not just add the third parameter to my_cos1 and replace whatever instance of 0.1 you have in there with eps? So you would call either my_cos1(x,N,0.1) or my_cos1(x,N,eps) depending on how many arguments you have.
function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K
Looks like that second post of yours happened while I was formatting your code and thinking about the answer. Whoops!
Is the "tayexp=r;" actually in the right spot? It looks like it will evaluate that statement for even when k=2, but it could be a matlab quirk.
It looks like right now, if you have 3 variables, it goes into the loop, will run N-1 times, adding to r whenever abs(t)>eps and doing nothing for the rest of the loop if abs(t)<eps (by omission, maybe you should break it if that case occurs), then you set tayexp=r (though that seems strange to me ... setting the value of a function to a variable ... I guess that way it always "evaluates" to one value, right?).
function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2 disp('Not enough of parameters') else x=varargin{1}; N=varargin{2}; if K==2 tayexp=my_cos1(x,N); else eps=varargin{3}; format long r = 0; for i = 0:N-1 t=((((-1)^i) * (x^(2*i)))/factorial(2*i)); if abs(t)>eps r=r+t; end end tayexp=r; end end end
Okay, the algorithm looks good (i.e. if abs(t)>eps, we add t to our running total, if abs(t)<=eps, we do nothing and continue evaluating the for loop), though you might want to just break the loop from further processing if you ever qualify the second scenario.
The only thing I could see that looked bad to me was that it appears you have an extra end following tayexp=r (see my formatted code above). I use notepad++ as my editor to make stuff like this easy to see, but the matlab editor may make it look pretty too.
Also, does "end" behave like a close parenthesis to the statement above it? for example
for i<4 ( if abs(t)>eps i++; )
if 1