Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
interpolation
%To view plots after 'plot' (and other plot-producing commands) this command must follow: 'print -dpng some_unique_plot_name.png;' %It exports current plot to png image which then is sent to your browser %GNU Octave 4.2.2 function l = lagrange(xj,xe,f) n = length(xj); l = 0.0; for j = 1:n l1 = 1; u1 = 1; for k = 1:j-1 l1 = l1.*(xe-xj(k)); u1 = u1*(xj(j)-xj(k)); end for k = j+1:n l1 = l1.*(xe-xj(k)); u1 = u1*(xj(j)-xj(k)); end l = l + (l1/u1)*f(j); end end p = 1000; N = 10; for n = 2:N xe = -1 + 2*(0:p)/p; xlj = -1 + 2*(0:n)/n; xcj = cos(((2*(0:n)+1)*pi)/(2*n+2)); f1 = @(t) sin(t); lf1 = lagrange(xlj,xe,f1(xlj)); cf1 = lagrange(xcj,xe,f1(xcj)); f1_el(n) = max(abs(lf1-f1(xe))); f1_ec(n) = max(abs(cf1-f1(xe))); f2 = @(t) sin(abs(t)); lf2 = lagrange(xlj,xe,f2(xlj)); cf2 = lagrange(xcj,xe,f2(xcj)); f2_el(n) = max(abs(lf2-f2(xe))); f2_ec(n) = max(abs(cf2-f2(xe))); f3 = @(t) sin(t.*sqrt(abs(t))); lf3 = lagrange(xlj,xe,f3(xlj)); cf3 = lagrange(xcj,xe,f3(xcj)); f3_el(n) = max(abs(lf3-f3(xe))); f3_ec(n) = max(abs(cf3-f3(xe))); f4 = @(t) 1./(1+25*t.*t); lf4 = lagrange(xlj,xe,f4(xlj)); cf4 = lagrange(xcj,xe,f4(xcj)); f4_el(n) = max(abs(lf4-f4(xe))); f4_ec(n) = max(abs(cf4-f4(xe))); end subplot(2,2,1), plot(xe,lf1); hold on; plot(xe,cf1); plot(xe, f1(xe)); title('f(x)=sin(x)'); subplot(2,2,2), plot(xe,lf2); hold on; plot(xe,cf2); plot(xe, f2(xe)); title('f(x)=sin(|x|)'); subplot(2,2,3), plot(xe,lf3); hold on; plot(xe,cf3); plot(xe, f3(xe)); title('f(x)=sin(x|x|^{1/2})'); subplot(2,2,4), plot(xe,lf4); hold on; plot(xe,cf4); plot(xe, f4(xe)); title('f(x)=1/(1+25x^2)'); print -dpng saif.png; fprintf('\nConvergence table for f(x)=sin(x): equispaced and chebyshev\n'); fprintf('2\t %e\t -- \t\t\t %e\t --\n', f1_el(2), f1_ec(2) ); for n = 3:N fprintf('%d\t %e\t %e\t\t %e\t %e\n', n, ... f1_el(n), log(f1_el(n-1)/f1_el(n))/log(n/(n-1)), ... f1_ec(n), log(f1_ec(n-1)/f1_ec(n))/log(n/(n-1))); end fprintf('\n'); fprintf('\nConvergence table for f(x)=sin(|x|): equispaced and chebyshev\n'); fprintf('2\t %e\t -- \t\t\t %e\t --\n', f2_el(2), f2_ec(2) ); for n = 3:N fprintf('%d\t %e\t %e\t\t %e\t %e \n', n, ... f2_el(n), log(f2_el(n-1)/f2_el(n))/log(n/(n-1)), ... f2_ec(n), log(f2_ec(n-1)/f2_ec(n))/log(n/(n-1))); end fprintf('\n'); fprintf('\nConvergence table for f(x)=sin(x|x|^{1/2}): equispaced and chebyshev\n'); fprintf('2\t %e\t -- \t\t\t %e\t --\n', f3_el(2), f3_ec(2) ); for n = 3:N fprintf('%d\t %e\t %e\t\t %e\t %e \n', n, ... f3_el(n), log(f3_el(n-1)/f3_el(n))/log(n/(n-1)), ... f3_ec(n), log(f3_ec(n-1)/f3_ec(n))/log(n/(n-1))); end fprintf('\n'); fprintf('\nConvergence table for f(x)=1/(1+25x^2): equispaced and chebyshev\n'); fprintf('2\t %e\t -- \t\t\t %e\t --\n', f4_el(2), f4_ec(2) ); for n = 3:N fprintf('%d\t %e\t %e\t\t %e\t %e \n', n, ... f4_el(n), log(f4_el(n-1)/f4_el(n))/log(n/(n-1)), ... f4_ec(n), log(f4_ec(n-1)/f4_ec(n))/log(n/(n-1))); end fprintf('\n');
run
|
edit
|
history
|
help
0
MATRIX MANIPULATIONS
wn pracs
test1.m
Arnoldi.m
Krylov.m
first.m
demo1
Test
first.c
Colebrook solver