Matrice
mikado
-
nido -
nido -
Bonjour,
voici la fonction que j'essaie d'executer
function [uh] = hermite(n,X,U,dU,x)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
a=length(x);
polynome=ones(n);
uh=ones(a);
for i=1:(n)
A=[(X(i)).^3 (X(i))^2 X(i) 1;(X(i+1)).^3 (X(i+1))^2 X(i+1) 1;3*(X(i))^2 2* X(i) 1 0;3*(X(i+1))^2 2* X(i+1) 1 0];
B=[U(i);U(i+1);dU(i);dU(i+1)];
polynome(i)= A\B;
end
for j=1:a
k=max(find(X<x(j)));
if k==0
k=1;
end
C=polynome(k);
uh(j)=C(1)*(x(j))^3+C(2)*(x(j))^2+c(3)*(x(j))+c(4);
end
end
et voici la reponse :
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in hermite (line 11)
polynome(i)= A\B;
Error in test_matlab3 (line 10)
uh = hermite(n,X,U,dU,x);
que puis je faire?
voici la fonction que j'essaie d'executer
function [uh] = hermite(n,X,U,dU,x)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
a=length(x);
polynome=ones(n);
uh=ones(a);
for i=1:(n)
A=[(X(i)).^3 (X(i))^2 X(i) 1;(X(i+1)).^3 (X(i+1))^2 X(i+1) 1;3*(X(i))^2 2* X(i) 1 0;3*(X(i+1))^2 2* X(i+1) 1 0];
B=[U(i);U(i+1);dU(i);dU(i+1)];
polynome(i)= A\B;
end
for j=1:a
k=max(find(X<x(j)));
if k==0
k=1;
end
C=polynome(k);
uh(j)=C(1)*(x(j))^3+C(2)*(x(j))^2+c(3)*(x(j))+c(4);
end
end
et voici la reponse :
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in hermite (line 11)
polynome(i)= A\B;
Error in test_matlab3 (line 10)
uh = hermite(n,X,U,dU,x);
que puis je faire?
1 réponse
Salut
L'erreur vient du fait que tu essaies d'assigner un vecteur à un scalaire, à cette ligne
polynome(i)= A\B;
Je pense qu'il te faudrait plutôt écrire quelque chose comme
polynome=ones(4,n);
etc.
polynome(:,i)=A\B;
etc.
C=polynome(:,k);
uh(j)=C(1)*(x(j))^3+C(2)*(x(j))^2+C(3)*(x(j))+C(4);
etc.
Je n'ai pas regardé plus que ça si le reste était correct. Avec des conditions de raccord un peu différentes aux breakpoints, tu pourrais directement utiliser pchip ou spline. Sinon, ta fonction peut aussi s'écrire
L'erreur vient du fait que tu essaies d'assigner un vecteur à un scalaire, à cette ligne
polynome(i)= A\B;
Je pense qu'il te faudrait plutôt écrire quelque chose comme
polynome=ones(4,n);
etc.
polynome(:,i)=A\B;
etc.
C=polynome(:,k);
uh(j)=C(1)*(x(j))^3+C(2)*(x(j))^2+C(3)*(x(j))+C(4);
etc.
Je n'ai pas regardé plus que ça si le reste était correct. Avec des conditions de raccord un peu différentes aux breakpoints, tu pourrais directement utiliser pchip ou spline. Sinon, ta fonction peut aussi s'écrire
function [u,du]=hermite(X,U,dU,x)Avec la Curve Fitting Toolbox, ça s'écrit plus simplement
X=X(:);U=U(:);dU=dU(:);n=length(X);m=n-1;
p=2*m;q=3*m+1;r=4*m;s=10*m+1;t=14*m;
dX=X(2:n)-X(1:m);M=zeros(r,4);M(s:t)=1;
M(n:p,1:3)=[dX.^3,dX.^2,dX];
M(q:r,1:2)=bsxfun(@times,[3,2],M(n:p,2:3));
M=mat2cell(reshape(M.',r,4),repmat(4,m,1),4);
YdY=num2cell([U(1:m),U(2:n),dU(1:m),dU(2:n)],2);
u=cellfun(@(m,ydy)(ydy/m),M,YdY,'UniformOutput',0);
du=cellfun(@(v)(polyder(v)),u,'UniformOutput',0);
u=mkpp(X,cell2mat(u));du=mkpp(X,cell2mat(du));
u=ppval(u,x);du=ppval(du,x);
end
function [u,du]=hermite(X,U,dU,x)Exemple d'utilisation
X=augknt(X(:),4,2);YdY=[U(:),dU(:)].';YdY=YdY(:);
B=spapi(X,X(3:end-2),YdY);u=fnval(B,x);du=fnval(fnder(B),x);
end
X=linspace(0,2*pi,8);U=sin(X);dU=cos(X);À l'avenir, tâche de chercher de l'aide ailleurs, il n'y a que des glands sur ce site minable.
x=linspace(0,2*pi,32);[u,du]=hermite(X,U,dU,x);
figure;hold on;plot(X,U);plot(x,u,'r');hold off;
figure;hold on;plot(X,dU);plot(x,du,'r');hold off;