Newton polynomial interpolating points, matrix too big
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi,
My code to make a newton polynomial and plot it does not work. I cannot figure out what the problem is. "Matrix dimensions must agree". It seems my matrix A grows too much.
f = @(x) x.^2.*sin(x);
x = [0 1 2 3 5 7 8];
y= f(x);
k = length(x)
ak=ones(k,1);
A = ak;
for column=2:k
ak = ak.*(x-x(column-1));
A =[A ak];
end
c=A\y
p =@(x) c(1) + c(2)*x + c(3)*x.^2+c(4)*x.^3;
tv = 0:0.1:6;
plot(tv, p(tv), 'r')
hold on;
plot(tv, f(tv), 'b')
채택된 답변
Alex Sune
2019년 5월 23일
Try changing this line
c=A\y
by
c=A\y'
댓글 수: 10
Thank you Alex.
Do you know why my interpolation ended up completely flat?
The problem is in dimensions
x = [0 1 2 3 5 7 8]; % row
ak=ones(k,1); % column
ak = ak.*(x-x(column-1)); % multiplying column.*row try: ak .* (x-x(column-1))'
c=A\y'
p =@(x) c(1) + c(2)*x + c(3)*x.^2+c(4)*x.^3; % using only 4 constants of 7?
Little suggestion:
A = zeros(length(x)); % pre-allocating
A(:,column) = ak;
for column=2:k
ak = ak.*(x-x(column-1))';
A(:,column) = ak;
end
I tried to update the code according to your suggestions and I got a singular matrix unfortunately.
f = @(x) x.^2.*sin(x);
x = [0 1 2 3 5 7 8];
y= f(x);
k = length(x)
A = zeros(length(x));
ak=ones(k,1);
A(:,column) = ak;
for column=2:k
ak = ak.*(x-x(column-1))';
A(:,column) = ak;
end
A
c=A\y'
p =@(x) c(1) + c(2).*x + c(3).*x.^2+ c(4).*x.^3 + c(5).*x.^4+ c(6).*x.^5 + c(7).*x.^5;
tv = 0:0.1:6;
plot(tv, p(tv), 'r')
hold on;
plot(tv, f(tv), 'b')
A(:,column) = ak; % what 'column' equals to?
for column=2:k
ak = ak.*(x-x(column-1))';
A(:,column) = ak;
end
There is a mistake near c(7) (must be c(7)*x.^6)
p = @(x) c(1) + c(2).*x + c(3).*x.^2+ c(4).*x.^3 + c(5).*x.^4+ c(6).*x.^5 + c(7).*x.^5;
Hi, it still does not give me a curve.
You didn't answer my question: "What 'column' variable equals to" in line before loop?
A(:,column) = ak;
Sorry, this line is not my code so I don't know what it refers to?
My code:
k = length(x)
ak=ones(k,1);
A = ak;
You change your code according to my suggestion but forgot to assign:
column = 1;
A(:,column) = ak;
When you run your code the value 'column' is probably: column = k = length(x) (from previous run)
Also, this part can be automated:
p = @(x) c(1) + c(2).*x + c(3).*x.^2+ c(4).*x.^3 + c(5).*x.^4+ c(6).*x.^5 + c(7).*x.^5;
% can be
p1 = tv*0; % pre-allocating
for i = 1:length(p1)
for j = 1:length(c)
p1(i) = p1(i) + c(j)*x(i)^(j-1);
end
end
plot(tv,p1,'r')
Thank you very much for this thorough answer.
♥
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
