Sub function not replacing syms variable x with input number
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Maryam Ansari
il 9 Feb 2016
Risposto: Walter Roberson
il 9 Feb 2016
My code isn't finished yet, but this is what I have
function findTaylorSeries = TaylorCos0(valueForX,termNum)
%%formula for taylor series is sum of (n!)^-1* nth derivative of f(x)*(x-a)^2
%%where 'a' represents the center of the taylor series
%%here the taylor series is xNot = 0 = a
n = 0; %%starting at term = 0;
syms x; %%creating a symbolic variable x in order to calculate derivative of function wrt x
findTaylorSeries = 2*cos(4*x);
while n <= termNum
taylor = (factorial(n)^-1)*(diff(findTaylorSeries,n))*x^(n);
%%display(taylor);
n = n+1;
end
subs(taylor,valueForX); %%subs value for x with inputX number
display(taylor);
when I run it using another script m file, I use the inputs TaylorCos0(1,2). It iterates to the second term, which is good. My answer = -16*cos(4*x)*x^2. By using sub, I want to replace all the x values with 1 and evaluate my expression. However, the output I still receive is "-16*cos(4*x)*x^2." The symbolic x variable still remains, and I don't understand why or how to fix it.
0 Commenti
Risposta accettata
Walter Roberson
il 9 Feb 2016
You need to tell subs which variable is to be substituted for. subs(taylor, x, valueForX)
Also you are discarding the result of the substitution and displaying the original expression. subs() does not change the expression being substituted into.
Thirdly, you should be totaling the terms you calculate.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Numbers and Precision in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!