Solving a problem with the Linear Least Squares Method
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have some problem with this code.
The approximate solution obtained does not coincide with the exact one provided by the professor.
Can you kindly help me?
Thanks in advance
%For testing
[x,u]=LinearLeastSquares(5,20);
y=(2.*x+3)./(1+x);
plot(x,u,'r*',x,y,'k-');
function [x,u]=LinearLeastSquares(m,nv)
%Lezione 19 novembre
%Least squares method (linear problem)
%Input
%m: number of the elements in the base
%(maximum value of m is 10)
%nv:number of visualization points
%Equation:
%-y''+2/(1+x)*y'+4/(3*(1+x)^2)*y=(8x)/(3(1+x)^3)
%BC:
%y(-1/2)=4 (essential)
%y(1)+10y'(1)=0 (natural)
%Exact solution: y=(2.*x+3)./(1+x);
lb=-1/2;
rb=1;
h=(rb-lb)/nv;
x=lb:h:rb; %x is a row
delta=zeros(m+2,1); %we have m+1 elements
A=zeros(m+2);
b=zeros(m+2,1);
for j=0:m
for l=0:m
A(j+1,l+1)=integral (@(x)fA1(x,l,j),-1/2,1);
end
A(j+1,m+2)=integral (@(x)fA2(x,m,j),-1/2,1);
b(j+1)= integral (@(x) fB(x,j),-1/2,1);
end
for l=0:m
A(m+1,l+1)= fu(l,-1/2); %from the boundery condition, 1st condition
A(m+2,l+1)= fu(l,1)-10.*du(l,1); %2nd condition
end
b(m+1)=4;
b(m+2)=0;
delta=A\b; % risoluzione del sistema lineare
u = zeros(1, nv+1);
for l = 0:m
u = u + delta(l+1) .* fu(x,l);
end
%evaluation of the solution at n points of the interval (-1/2,1)
u=u+U(x,m);
end
function y=fu(x,l)
%function for the elements
y=x.^l.*(2.*x+1);
end
function y=du(x,l)
%first derivative of the base
y=(l+1).*2.*x.^(l)+(l).*x.^(l-1);
end
function y=ddu(x,l)
%second derivative of the base
y=(l+1).*(2.*l).*x.^(l-1)+(l).*(l-1).*x.^(l-2);
end
function y=fv(x,m)
y=x.^(m+1).*(2.*x+1);
end
function y=dv(x,m)
y=2.*x.^(m+1).*(m+2)+x.^m.*(m+1);
end
function y=ddv(x,m)
y=2.*(m+1).*x.^m+m.*(m+1).*x.^(m-1);
end
function y=fC(x,l)
y=-ddu(x,l)+2./(1+x).*du(x,l)+4./(3.*(1+x).^2).*fu(x,l);
end
function y=V1(x,m)
y=-ddv(x,m)+2./(1./x).*dv(x,m)+4./(3.*(1+x.^2)).*fv(x,m);
end
function y=fA1(x,l,j)
y=fC(x,l).*fC(x,j);
end
function y=fA2(x,m,j)
y=V1(x,m).*fC(x,j);
end
function y=F(x)
y=4./(3.*(1+x).^2).*4-(8.*x)./(3.*(1+x).^3);
end
function y=fB(x,j)
y=F(x).*fC(x,j);
end
function y=U(x,m)
%gamma?
y=V1(x,m);
end
0 Kommentare
Antworten (1)
Nipun
am 4 Okt. 2023
Hi Francesca,
I understand that you are trying to align the output to the attached code for the Linear Least Squares method implementation with the expected output.
I assume that you have a predefined function for the given inputs on which you are trying to apply the least squares method. I recommend that you use the linear least squares method toolbox provided by MATLAB. Symbolic representation is accepted for problem-based approach. I am attaching the documentation to the toolbox with this answer.
There are different functions depending upon whether you choose solver based approach or problem based approach. You may use the solve function to get the desired output.
Link to documentation: Linear Least Squares - MATLAB & Simulink - MathWorks India
Hope this helps.
Regards,
Nipun
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
