MATLAB Answers

Insert number in polynomial

2 views (last 30 days)
Chr Mil
Chr Mil on 17 Jan 2020
Answered: Sergey Kasyanov on 17 Jan 2020
So I have the polynomial p with syms x and I want the user to give me a number and calculate the polynomial at that point. The problem is that I cannot declare p as polynomial with vector because I don't know the degree. Here's my code :
clc
clear all;
syms x
x_val=[0 -pi/4 -pi/3 -pi/2 pi pi/6 pi/4 pi/3 pi/2 -pi];
y_val=[0 -sqrt(2)/2 -sqrt(3)/2 -1 0 1/2 sqrt(2)/2 sqrt(3)/2 1 0];
g=input('Give corner in raduis (from [p,-p])');
for i=1:10
ar=1;
par=1;
for j=1:10
if i==j
continue %Αν το i==j τοτε να μην υπολογιστεί τίποτα γιατί ο παρονομαστής θα γίνει 0 και ο αριθμητής δεν περιέχει το (x-x1) στο L1
else
ar=ar*(x-x_val(j));
par=par*(x_val(i)-x_val(j));
end
end
L(i)=ar/par;
end
p=0;
for i=1:10
p=p+(y_val(i)*L(i));
end
f= @(x)p;
disp(f(g))

  0 Comments

Sign in to comment.

Answers (1)

Sergey Kasyanov
Sergey Kasyanov on 17 Jan 2020
Hello,
You need to replace f= @(x)p; by f = symfun(p,x);.

  0 Comments

Sign in to comment.

Sign in to answer this question.