Recurrence Relation having undefined variables

Hi there
I am trying to run a recurrence relation from an ODE as doing the calculations by hand is turning out to be long. I am having some trouble doing so. I am getting lots of errors no matter what I try. I feel like this code is correct:
syms lambda c0 c1
for k = 1:1
c(k)
end
function c = c(k)
if k == 0
c = c0;
elseif k ==1
c = c1;
elseif k == 2
c = -lambda * c0/2
else
c = (-c(0) * ((-1)^((k - 1)/2) * (pi/L)^k)/(factorial(k)) - symsum(((-1)^((l - 1)/2) * (pi/L)^l)/(factorial(l)) * c(k+1-l),l,1,k) - lambda * c(k))/((k+2)*(k+1));
end
end
The for loop has k = 1:1 for now just for testing purposes. Eventually I will make it higher. I am getting the following errors:
"unrecognized function or variable 'c1'.
Error in (filename)>c
c = c1;
error in (filename)
c(k)
The issues appear to be with c1, but I don't understand how because I made it symbolic (I do have symbolic math toolbox installed), and I want to keep it as a variable. If I change c1 to a number, now the code starts having issues with lambda! If I make lambda, c0, and c1 all numbers, I get "unrecognized function or variable 'c'."
I am lost! I feel as though this errors should not be happening because I did define these variables. What is going on?

 Respuesta aceptada

Paul
Paul el 6 de Sept. de 2021
Do you get the desired result if you move the line
syms lamda c0 c1
to inside the function?
Also it's kind of confusing to have the output of the functin have the same name as the function itself. Even if it is allowed.

8 comentarios

Dominic Blanco
Dominic Blanco el 7 de Sept. de 2021
This does not work beyond k = 1:2. If I run k = 1:3, it gives "unrecognized function or variable 'c.'"
Paul
Paul el 7 de Sept. de 2021
I thought it might be because the parser is confused between the output argument and the name of the function. Also, the line that starts
c = ( -c(0) ....
has other undefined variables, e.g. L and l.
Mabe if you post the mathematical equations someone would be able to help with the implementation.
Dominic Blanco
Dominic Blanco el 7 de Sept. de 2021
The equation is
Let me know if this helps.
Paul
Paul el 7 de Sept. de 2021
Based on this equation, is it correct that k > = 0? That is C0 and C1 are known, and this equation is for C2 (with k = 0), C3 (k = 1), etc.?
But if k = 0 is valid, how does one intepret that summation from l = 1 to k when k = 0?
Dominic Blanco
Dominic Blanco el 7 de Sept. de 2021
The formua starts with k = 1. That's why it's written as c_(k + 2). This is a recurrence relation for an ode done with power series. The l part is the Cauchy product.
Sorry, I should have seen from the original Question that C2 is defined.
Instead of using recursion, maybe a loop will be simpler to understand
c = computec(5)
c = 
simplify(c)
ans = 
I think I implemented that equation correctly. Make sure to double check.
function c = computec(n)
syms c_0 c_1 lambda L l
c = sym(zeros(n+1,1));
c(0 +1) = c_0;
c(1 +1) = c_1;
c(2 +1) = -lambda*c_0/2;
for k = 1:n-2
l = (1:k).';
c(k+2 +1) = ...
- c_0*(-1)^((k-1)/2)*(sym(pi)/L)^k/factorial(k) ...
- sum( (-1).^((l-1)/2).*(sym(pi)/L).^l./factorial(l) .* c( (k+1-l) + 1) ) ...
- lambda*c(k +1);
c(k+2 +1) = c(k+2 +1)/(k+2)/(k+1);
end
end
Dominic Blanco
Dominic Blanco el 9 de Sept. de 2021
This does indeed work! I was also able to find one mistake in my work done by hand, which was the goal of me writing this. If all but one of the terms matches my handwritten work, I believe this is correct! I did accept your answer as this does answer my question.
If I am to be more picky, is there a way to determine how the output behaves? To explain what I mean, most of the terms in c_5's output have things factored out and fractions written weirdly. Most of the terms have (something/6 /20L. I would love for that to say 120L instead of 6/20L. Not all terms behaves like this, as there is one that has 120 in the denominator, but is there a way to make them all behave the same way? No worries if not, this is still very helpful!
Paul
Paul el 9 de Sept. de 2021
Editada: Paul el 9 de Sept. de 2021
Hi Dominic,
I'm not sure what you mean by the "something/6/20L" in the context of any of the output above. Maybe you're seeing something different in the Matlab command window as compared to how Live Script (which I think is what Answers uses) formats its output? In any case, the Symbolic Math Toolbox offers various functions for simplifying and/or manipulating and/or rewriting expressions (search the doc for "Fomula Manipulation and Simplification"), so I suspect you can get to the form that you want. If still having trouble, post here (or in a new question) and show the specific expression of concern and describe what the desired end product should look like.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 5 de Sept. de 2021

Editada:

el 9 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by