# Recurrence Relation having undefined variables

3 views (last 30 days)
Dominic Blanco on 5 Sep 2021
Edited: Paul on 9 Sep 2021
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?

Paul on 6 Sep 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.
Paul on 9 Sep 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.

R2020b

### Community Treasure Hunt

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

Start Hunting!