I'm calculating using the order 3 polynomial interpolation method. But why does an array appear?
2 ビュー (過去 30 日間)
古いコメントを表示
%polinomial
disp('polinomial interpolation')
clear
clc
x=1.3;
x1=0.0;
x2=0.5;
x3=1.0;
x4=1.5;
y1=log(x1);
y2=log(x2);
y3=log(x3);
y4=log(x4);
m3=log(x);
% dif 1
xa=(y2-y1)/(x2-x1);
xb=(y3-y2)/(x3-x2);
xc=(y4-y3)/(x4-x3);
% dif 2
xd=(xb-xa)/(x3-x1);
xe=(xc-xb)/(x4-x2);
% dif 3
xf=(xe-xd)/(x4-x1);
% f(1.3)
f(x)= (x1)+(xa)*(x-x1)+(xd)*(x-x1)*(x-x2)+(xf)*(x-x1)*(x-x2)*(x-x3);
fprintf ('%f\n',(f(x)))
et=(m3-f(x))/m3*100;
it says that theres an error in line 30 and i dont know how to fix it. please help
0 件のコメント
回答 (2 件)
Dyuman Joshi
2023 年 11 月 20 日
The syntax for defining an anonymouts function is incorrect in your code.
Also, you are trying to take logarithm of 0, which does not lead to good things, see below -
clear
clc
%polinomial
disp('polinomial interpolation')
x=1.3;
x1=0.0;
x2=0.5;
x3=1.0;
x4=1.5;
y1=log(x1)
y2=log(x2);
y3=log(x3);
y4=log(x4);
m3=log(x);
% dif 1
xa=(y2-y1)/(x2-x1);
xb=(y3-y2)/(x3-x2);
xc=(y4-y3)/(x4-x3);
% dif 2
xd=(xb-xa)/(x3-x1);
xe=(xc-xb)/(x4-x2);
% dif 3
xf=(xe-xd)/(x4-x1);
% f(1.3)
%% Corrected Syntax
f = @(x) (x1)+(xa)*(x-x1)+(xd)*(x-x1)*(x-x2)+(xf)*(x-x1)*(x-x2)*(x-x3);
fprintf('%f\n',f(x))
et=(m3-f(x))/m3*100
0 件のコメント
Steven Lord
2023 年 11 月 20 日
When you receive warning and/or error messages please show the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window.) The exact text of the message may be useful in determining what's going on and how to avoid the warning and/or error.
In this case I suspect I know the cause of the error.
f(x)= (x1)+(xa)*(x-x1)+(xd)*(x-x1)*(x-x2)+(xf)*(x-x1)*(x-x2)*(x-x3);
You can't assign to element 1.3 of an array. If you want f to be a function you can call with a specific value of x as input, define it as an anonymous function.
f = @(x) (x1)+(xa)*(x-x1)+(xd)*(x-x1)*(x-x2)+(xf)*(x-x1)*(x-x2)*(x-x3);
But you should use array multiplication, not matrix multiplication here so your function is vectorized, can be called with a vector or array as input not just a scalar.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!