while loop not taking decimal or fractional answers and unrecognized variable

Hi there
I am trying to write a while loop to determine convergence of a Taylor and/or Laurent series. I am not being assigned to do this, I just want to do it for myself as it would be very useful. I've found codes online for "normal" series, but I can't find one that works for a form of power series. On top of this though, I am having issues with the "normal" series code. I have started with this code I found online.
ER=100;
s=0;
n=1;
while (ER >= 0.01)
n_sum = (1/(n^2));
s = s + n_sum; %compute the running sum
ER =(abs(((pi^2)/6)-s)/((pi^2)/6))*100; %Calculate error
n = n + 1; %counter
end
disp(s)
disp(n-1)
disp(ER)
This is very useful as it calculates the error, number of times it took to converge, and the point it converged to. I want to make this more general and input 1.6448 rather than (pi^2)/6. I made the change as follows:
ER=100;
s=0;
n=1;
while (ER >= 0.01)
n_sum = (1/(n^2));
s = s + n_sum; %compute the running sum
ER =(abs(((1.6448)-s)/((1.6448))*100; %Calculate error
n = n + 1; %counter
end
disp(s)
disp(n-1)
disp(ER)
This, however, gives me an "invalid expression, when calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched dlimiters." If I enter it as a fraction, it also does not work. I am struggling to understand why. I attempted changing the error as follows as I read somewhere that there could be an issue with symbolic language? I don't know what that is as I'm fairly new, but I tried, and it didn't work.
ER=100;
s=0;
n=1;
while (double(subs(ER)) >= 0.01)
n_sum = (1/(n^2));
s = s + n_sum; %compute the running sum
ER =(abs(((1.6448)-s)/((1.6448))*100; %Calculate error
n = n + 1; %counter
end
disp(s)
disp(n-1)
disp(ER)
This also significantly slowed down the code if I put (pi)^2/6 back in.
The main goal of this is also causing errors. If I try to input a Laurent series as follows:
ER=100;
s=0;
n=1;
while (double(subs(ER)) >= 0.01)
n_sum = (x/(n*(x-n)));
s = s + n_sum; %compute the running sum
ER =(abs(((1.6448)-s)/((1.6448))*100; %Calculate error
n = n + 1; %counter
end
disp(s)
disp(n-1)
disp(ER)
I am also getting an error: "Unrecognized variable 'x'." I can't fix this as x is not defined. It's the variable in the power series. Sorry for all of the code, I would have uploaded photos if I could. Any help is appreciated!

 Accepted Answer

ER =(abs(((1.6448)-s)/((1.6448))*100; %Calculate error
1 234 3 2 34 32
The digit is the number of unmatched open-brackets "after" the character above it.
I am also getting an error: "Unrecognized variable 'x'." I can't fix this as x is not defined. It's the variable in the power series.
Are you trying to build a formula ? If so then you can use the Symbolic Toolbox -- but if you do, then you cannot calculate an absolute error -- the absolute error is going to depend upon the input x.
I am trying to write a while loop to determine convergence of a Taylor and/or Laurent series.
You cannot use that approach for unknown inputs.
For known inputs, you can calculate whether a new term is going to be insignificant relative to the current sum, but that approach cannot be used to demonstrate convergence. Consider for example the series sum(1/n, n, 1, 10^16-1) which is roughly 37.418577 . The next term would be 10^(-16) which would be less than eps(37.418577) and therefore cannot add anything to the double precision number, and all remaining terms are even smaller . Using a while loop to test the value of the term would imply that sum(1/n,n,1,inf) is convergent.. but it is not. sum(1/n,n,1,x) converges to log(x)+eulernumber and log(inf) is infinity . So even though if you calculate in double precision going forward from n=1, the terms after 10^16 cannot add anything to the sum, mathematically those remaining terms to n=infinity add up to infinity.
You need a different approach if you want to establish convergence.

3 Comments

Could you suggest to me how I could go about that? I have been struggling to find one. My main goal is to have convergence of Taylor and Laurent series.
syms x n m
S = symsum(x/(n*(x-n)),n,1,m)
S = 
R = rand() * 5
R = 2.3960
Sx = subs(S, x, R)
Sx = 
limit(Sx, m, inf)
ans = 
subs(S, x, 7)
ans = 
limit(ans,m,inf)
ans = 
NaN
So no convergence if x is a positive integer. If it is not, then you need to prove that psi(m+1)-psi(m-x+1) converges . And to use your approach, you would have to do the proof of convergence using a while loop . For all permitted non-integer x.
You need a different approach if you are wanting to prove it for all permitted (non-integer) x.
Got it. And this makes sense because (x-n) will be 0 if x is an integer, and that will cause a singularity. Thank you for your help! I will try to modify this to make it work for x non-integers.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!