Hello all, I coded a nodal basis function for 1D element from [-1,1]. the code is below:
close all; clc; clearvars;
n=10;
x = linspace(-1,1,n);
for i=1:n
a= x(i);
for j=1:n
b(j)=a.^(j-1);
end
v(i,:)=b';
end
vinv=inv(v);
for i=1:n
k=zeros(1,n);
k(i)=1;
f=vinv*k'
p(:,i)=f;
end
for i=1:n
g=@(x) p(1,i)+p(2,i).*x+p(3,i).*x.^2+p(4,i).*x.^3+p(5,i).*x.^4+p(6,i).*x.^5+p(7,i).*x.^6+p(8,i).*x.^7+p(9,i).*x.^8+ ....
p(10,i).*x.^9;
legendInfo{i} = ['Phi ' num2str(i)];
fplot(g, [-1 1])
legend(legendInfo)
hold on;
end
The code works already but my problem is in last "for loop" where I calculated "g" as a function handle. I want to instead of adding the terms from 1 to 10, use an automated calculation. Now, if I want to change number of nodes (n) from 10 to 20 I have to add 10 additional terms by hand. Moreover, Does somebody knows a better way to calculate nodal basis function for 1D element? Great thanks,

 채택된 답변

KSSV
KSSV 2018년 10월 25일

1 개 추천

Read about polyval
n=10;
x = linspace(-1,1,n);
for i=1:n
a= x(i);
for j=1:n
b(j)=a.^(j-1);
end
v(i,:)=b';
end
vinv=inv(v);
for i=1:n
k=zeros(1,n);
k(i)=1;
f=vinv*k'
p(:,i)=f;
end
x = linspace(-1,1,1000) ;
for i=1:n
r = flipud(p(:,i)) ;
y = polyval(r,x) ;
legendInfo{i} = ['Phi ' num2str(i)];
plot(x,y)
legend(legendInfo)
hold on;
end

댓글 수: 4

mohamad hoseini
mohamad hoseini 2018년 10월 25일
Thanks for your response. The code works well by polyval. Do you know it is possible to make this polynomials by symbolic functions depend on (x)?
KSSV
KSSV 2018년 10월 25일
Have a look on poly2sim
mohamad hoseini
mohamad hoseini 2018년 10월 25일
Thanks
KSSV
KSSV 2018년 10월 25일
Thanks is accepting and voting the answer..:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2018년 10월 25일

댓글:

2018년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by