Formatting an input set of Values

1 view (last 30 days)
Logan Casey
Logan Casey on 10 Dec 2020
Commented: Uday Pradhan on 16 Dec 2020
In my Gauss Quadrature code, it requests 4 values be entered as inputs. I know what 3 of the 4 inputs are, but the first input is the entire base function. I am having two issues:
The first issue is I am struggling to properly format my input function so that it uses the correct equation
The second issue is if the code doesn't result in an error regarding the function, it comes up with an error saying I need to define my x and y values. Does anyone have a solution?
The function in question is:
sqrt(x'(t)^2 + y'(t)^2) integrated from a to b where a = 0 and b = 5
Posted below is my code:
%Integration using Gauss-Legendre quad
%Similar to 'integral' in MATLAB
function y=GLGD_int(fun,xmin,xmax,n)
%fun: the integrand as a function handle
%xmin: lower bound of integration
%xmax: upper bound for integration
%n: order of polynomials used
[x_IP,weight]=GLGD_para(n);
%assign global coordinates to the integration points
x_eval=x_IP*(xmax-xmin)/2+(xmax+xmin)/2;
y=0;
for aa=1:n
y=y+feval(fun,x_eval(aa))*weight(aa)*(xmax-xmin)/2;
end
end
function [x_IP,weight]=GLGD_para(n)
%n: order of the polynomial
x_IP=legendreRoot(n,10^(-16));
weight=2./(1-x_IP^2)./diff_legendrePoly(x_IP,n).^2
end
%roots of the Legendre Polynomial using Newton-Raphson
function x_IP=legendreRoot(n,tol)
%n: order of the polynomial
%tol: tolerance of the error
if n<2
disp('No root was found');
else
root=zeros(1,floor(n/2));
for aa=1:floor(n/2) %iterate to find half of the roots
x=cos(pi*(aa-0.25)/(n+0.5));
err=10*tol;
iter=0;
while (err>tol)&&(iter<1000)
dx=-legendrePoly(x,n)/diff_legendrePoly(x,n);
x=x+dx;
iter=iter+1;
err=abs(legendrePoly(x,n));
end
root(aa)=x;
end
if mod(n,2)==0
x_IP=[-1*root,root];
else
x_IP=[-1*root,0,root];
end
x_IP=sort(x_IP);
end
end
%derivative of the Legendre Polynomial
function y=diff_legendrePoly(x_IP,n)
%x_IP: coordinates of integration points
if n==0
y=0;
else
y=n./(x_IP.^2-1).*(x_IP.*legendrePoly(x_IP,n)-legendrePoly(x_IP,n-1));
end
end
%Produces Legendre Polynomials
function y=legendrePoly(x,n)
%x: input x
if n==0
y=1;
elseif n==1
y=x;
else
y=((2*n-1).*x.*legendrePoly(x,n-1)-(n-1)*legendrePoly(x,n-2))/n;
end
end
  1 Comment
Uday Pradhan
Uday Pradhan on 16 Dec 2020
The function in question is:
sqrt(x'(t)^2 + y'(t)^2) integrated from a to b where a = 0 and b = 5
So, x and y are functions of t, right? You need to define these to get rid of the errors.

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!