I can't understand what my mistakes are
41 ビュー (過去 30 日間)
古いコメントを表示
2 件のコメント
回答 (2 件)
Star Strider
2024 年 12 月 27 日 16:11
編集済み: Star Strider
2024 年 12 月 27 日 16:31
Using parentheses () to index into a table indexes the table itself rather than the elements of the table. To index the elements, use curly brackets {} instead. See the documentation section on Access Data in Tables for details.
t = array2table(rand(100, 5))
h = linspace(1, 100, 100);
l1 = prod(h-t{2:5,1}) ./ prod(t{1,1}-t{2:5,1})
l1_original = ((h-t{2,1}).*(h-t{3,1}).*(h-t{4,1}).*(h-t{5,1})) ./ ((t{1,1}-t{2,1}).*(t{1,1}-t{3,1}).*(t{1,1}-t{4,1}).*(t{1,1}-t{5,1}))
Check = all(l1 == l1_original) % ‘1’ (true) If All Elements Are Equal
Checking that this version of ‘l1’ produces the same result as your hard-coded version. (It should, and it does here.)
EDIT — (27 Dec 2024 at 16:25)
EDIT — (27 Dec 2024 at 16:31)
Now having the code and data —
%1 without overcomplicating
t = readtable('TabelaValores1');
h = linspace(1,100,100);
%em comum
l1=(((h-t{2,1}).*(h-t{3,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{1,1}-t{2,1}).*(t{1,1}-t{3,1}).*(t{1,1}-t{4,1}).*t{1,1}-t{5,1}));
l2=(((h-t{1,1}).*(h-t{3,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{2,1}-t{1,1}).*(t{2,1}-t{3,1}).*(t{1,1}-t{4,1}).*t{1,1}-t{5,1}));
l3=(((h-t{1,1}).*(h-t{2,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{3,1}-t{1,1}).*(t{3,1}-t{2,1}).*(t{3,1}-t{4,1}).*t{3,1}-t{5,1}));
l4=(((h-t{1,1}).*(h-t{2,1}).*(h-t{3,1}).*(h-t{5,1})) ...
./((t{4,1}-t{1,1}).*(t{4,1}-t{2,1}).*(t{4,1}-t{3,1}).*t{4,1}-t{5,1}));
l5=(((h-t{1,1}).*(h-t{2,1}).*(h-t{3,1}).*(h-t{4,1})) ...
./((t{5,1}-t{1,1}).*(t{5,1}-t{2,1}).*(t{5,1}-t{3,1}).*t{5,1}-t{4,1}));
%para o x
x1=t{1,2};
x2=t{2,2};
x3=t{3,2};
x4=t{4,2};
x5=t{5,2};
interpolx=(x1*l1+x2*l2+x3*l3+x4*l4+x5*l5);
%para o y
y1=t{1,3};
y2=t{2,3};
y3=t{3,3};
y4=t{4,3};
y5=t{5,3};
interpoly=(y1*l1+y2*l2+y3*l3+y4*l4+y5*l5);
figure
plot(interpolx, interpoly)
grid
.
This works after replacing the appropriate parentheses with curly brackets, and using element-wise multiplication.
.
2 件のコメント
Star Strider
2024 年 12 月 27 日 20:03
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Image Analyst
2024 年 12 月 27 日 15:51
Looks like line 6 is really not empty. Maybe scroll to the right to see if there is a - on that line.
Also t(2,1) I think is a table, not the contents of the table at that location. Try using {} instead of () for all your t references, like t{2,1}.
If you have any more questions, then attach your files (Time.m and TabelaValores1.xlsx) and code to read it in with the paperclip icon after you read this:
2 件のコメント
Image Analyst
2024 年 12 月 27 日 22:15
I'm glad my suggestion of using braces instead of parentheses got you further. You don't want to remove the . from .* because they are multi-element arrays, not scalars. So I put them back in and replaced the remaining parentheses with braces and it works fine. It usually doesn't hurt if you use .* instead of * but you need to make sure you want element-by-element multiplication, not matrix multiplication, and the two arrays are compatible sizes.
%1 without overcomplicating
t = readtable('TabelaValores1');
h = linspace(1,100,100);
%em comum
l1=(((h-t{2,1}).*(h-t{3,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{1,1}-t{2,1}).*(t{1,1}-t{3,1}).*(t{1,1}-t{4,1}).*t{1,1}-t{5,1}));
l2=(((h-t{1,1}).*(h-t{3,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{2,1}-t{1,1}).*(t{2,1}-t{3,1}).*(t{1,1}-t{4,1}).*t{1,1}-t{5,1}));
l3=(((h-t{1,1}).*(h-t{2,1}).*(h-t{4,1}).*(h-t{5,1})) ...
./((t{3,1}-t{1,1}).*(t{3,1}-t{2,1}).*(t{3,1}-t{4,1}).*t{3,1}-t{5,1}));
l4=(((h-t{1,1}).*(h-t{2,1}).*(h-t{3,1}).*(h-t{5,1})) ...
./((t{4,1}-t{1,1}).*(t{4,1}-t{2,1}).*(t{4,1}-t{3,1}).*t{4,1}-t{5,1}));
l5=(((h-t{1,1}).*(h-t{2,1}).*(h-t{3,1}).*(h-t{4,1})) ...
./((t{5,1}-t{1,1}).*(t{5,1}-t{2,1}).*(t{5,1}-t{3,1}).*t{5,1}-t{4,1}));
%para o x
x1=t{1,2};
x2=t{2,2};
x3=t{3,2};
x4=t{4,2};
x5=t{5,2};
interpolx=(x1*l1+x2*l2+x3*l3+x4*l4+x5*l5);
%para o y
y1=t{1,3};
y2=t{2,3};
y3=t{3,3};
y4=t{4,3};
y5=t{5,3};
interpoly=(y1*l1+y2*l2+y3*l3+y4*l4+y5*l5);
fprintf('DONE! It worked (or at least it finished)\n');
See this link for help in understanding when to use braces, parentheses, or brackets:
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
For full details on how to earn reputation points see: https://www.mathworks.com/matlabcentral/answers/help?s_tid=al_priv#reputation
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!