简体   繁体   中英

Lagrange Polynomial not plotting in Octave

The question requires a Lagrange polynomial L(c) to replace the complicated-looking function, f. Also, plot function f and L(c) between [1:0; 10:0]. However, after the code computes the values for k, it cannot seem to create a plot and a graph. What am I doing wrong? Attached is the code below. Thank You!

clear
pkg load symbolic
graphics_toolkit('gnuplot')
syms m x
c = []
for i = 1: 0.5 : 10
  c = [c,i]
endfor
d = length(c)
k = []
f = 3.* (1 .- ( 8 ./ vpa(pi.^2) .* ((1 ./ cosh(vpa(pi) .* vpa(x) ./ 2)) .+ symsum(1 ./ ((2 .* m .+ 1).^2 .* cosh((2 .* m .+ 1) .* vpa(pi) .* vpa(x) ./ 2)), m,1 ,Inf)))) ./ ((1 .- ( 192 ./ vpa(x) .* vpa(pi.^5)).*(tanh(vpa(pi) .* vpa(x) ./ 2) .+ symsum(tanh((2 .* m .+ 1) .* vpa(pi) .* vpa(x) ./ 2) ./ (2 .* m .+ 1).^5, m,1 ,Inf))));
for i = 1:d
  A = (1 - ( 192 / vpa(c(i)) * vpa(pi^5))*(tanh(vpa(pi) * vpa(c(i)) / 2) + symsum(tanh((2 * m + 1) * vpa(pi) * vpa(c(i)) / 2) / (2 * m + 1)^5, m, 1, Inf)))
  B = 1 - ( 8 / vpa(pi^2) * ((1 / cosh(vpa(pi) * vpa(c(i)) / 2)) + symsum(1 / ((2 * m + 1)^2 * cosh((2 * m + 1) * vpa(pi) * vpa(c(i)) / 2)), m, 1, Inf))) 
  K = 3 * B / A
  k = [k,K]
endfor
col = c(1)
matrixc = [];
for m = 1:d;
  prod = 1;    
  for t = 1:d
    if col == ((m+1)/2)
      col = col .+ ((10-1)/(d-1));
    endif   
    sol = (x.-vpa(col))./(vpa((m+1)/2).-vpa(col));
    prod = prod*sol;   
    col = col .+ ((c(2)-c(1))/(d-1));  
  endfor
  h = k(m);
  p = vpa(h).*prod;
  matrixc = [matrixc,p];
  col = 1;
endfor
L=sum(matrixc);
L
ezplot(L,[c(1),c(d)]);
hold on;
k(1)
plot(c,k);
legend('Lagrange Polynomial','K(x)');
xlabel('x-axis');
ylabel('y-axis');```


You are trying to plot a symbolic variable. I don't know if this is possible in general, but in this particular case at least the conversion is causing problems

Collect the values of the symbolic variable first, by casting to double , and then you can plot as intended:

plot( c, double(k) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM