Info

This question is closed. Reopen it to edit or answer.

Trouble creating function out of variable in terms of x

1 view (last 30 days)
For my project, I need to create a polynomial using divided differences. I sucessfuly can create the polynomial, but I do not know how to turn this polynomial into a funcion. I created this polynomial by adding to matrices together.
E= sym(zeros(n,1));
b=2;
d=1;
if b<n
for a=1:1:n
p=(x-B(1,a));
d=p*d;
E(a,1)=d;
end
b=b+1;
end
F= sym(zeros(n,1));
m=1;
while m<n+1
if m<2
F(m,1) = C(m,1);
end
if m>1
F(m,1) = C(m,1)*E(m,1);
end
m=m+1;
end
w=1;
poly=0;
while w<n+1
addon=F(w,1);
poly=poly+addon;
w=w+1;
end
  1 Comment
John Phelan
John Phelan on 11 Mar 2020
I get a correct polynomial ouput for "poly", but I cannot plug in an x term for poly. For example, poly(2) does not work. I am pretty sure that this is just because of the type of variable I am using.
Also I take numbers from matrices B and C which I do not include here.

Answers (1)

Peter O
Peter O on 11 Mar 2020
Is there any particular reason it needs to be using symbolic math?
MATLAB has a built-in polynomial evaluation function called polyval. See if you can structure your problem to take advantage of that for evaluation.
  1 Comment
John Phelan
John Phelan on 11 Mar 2020
I am not purposely using symbolic math. To be honest, I am very new to using MAT LAB.
However, I am not sure if using polyval is the best approach here. For my x values, I need them in the form (x-1)*(x-2)... etc. So I don't think it would be easy to covert that to the form needed for polyval
I am wondering how I can convert my answer in terms of x variables to the correct form that can be turned into a function.

Community Treasure Hunt

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

Start Hunting!