error in multiplying symbolic variable in a matrix
Afficher commentaires plus anciens
i have an important problem in writing a program for calculating Lagrange. i want to assume z as parameter. then I face this error: Error in MuPAD command: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead. this is my program:
clear
clc
syms z
x=[0 300 1000 1500 1700 2000 3000 3300 4000 4500 5000]
y=[32.0 122.4 296.4 405.7 447.6 608.4 704.7 761.4 891.9 983 1072.6]
m=z-x
i=1
j=1
L=[1 1 1 1 1 1 1 1 1 1 1]
q=[1 1 1 1 1 1 1 1 1 1 1]
while j<12
if j==1
for i=2:11
L(1)=L(1)-m(i)
q(1)=q(1)*(x(j)-x(i))
end
else
for i=1:(j-1)
L(j)=L(j).*m(i)
q(j)=q(j).*(x(j)-x(i))
end
for i=(j+1):11
L(j)=L(j).*m(i)
q(j)=q(j).*(x(j)-x(i))
end
end
j=j+1
end
e=(y.*L)
o=e./q
k=1
f=0
while k<12
f=f+o(k)
k=k+1
end
ezplot(f)
1 commentaire
Dyuman Joshi
le 31 Juil 2023
What is the final output you want?
Also, you can optimize your code -
1 - Club the 2 for loops under the else statement together -
for i = [1:(j-1) j+1:11]
L(j) = L(j).*m(i);
q(j) = q(j).*(x(j)-x(i));
end
After you combine them, you will see that you can vectorize them -
i = [1:(j-1) j+1:11];
L(j) = L(j)*prod(m(i));
q(j) = q(j)*prod(x(j)-x(i));
2 - replace the 2nd while loop by
f = sum(o(1:11));
You should also consider using semi-colons at the end of lines, to suppress the outcomes you don't want to get.
Réponses (1)
clear
clc
syms z L [1 11]
z , L
x=[0 300 1000 1500 1700 2000 3000 3300 4000 4500 5000];
y=[32.0 122.4 296.4 405.7 447.6 608.4 704.7 761.4 891.9 983 1072.6];
m=z-x
i=1;
j=1;
% L= [1 1 1 1 1 1 1 1 1 1 1];
q=[1 1 1 1 1 1 1 1 1 1 1];
while j<12
if j==1
for i=2:11
L(i)=L(i)-m(i);
q(i)=q(i)*(x(j)-x(i));
end
else
for i=1:j-1
L(j)=L(j).*m(i);
q(j)=q(j).*(x(j)-x(i));
end
for i=j+1:11
L(j)=L(j).*m(i);
q(j)=q(j).*(x(j)-x(i));
end
end
j=j+1;
end
e=(y.*L);
o=e./q;
k=1;
f=0;
while k<12
f=f+o(k) ;
k=k+1;
end
f
Catégories
En savoir plus sur Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
