I can't understand what my mistakes are

41 ビュー (過去 30 日間)
Alexandre Da Silva
Alexandre Da Silva 2024 年 12 月 27 日 15:38
コメント済み: Image Analyst 2024 年 12 月 27 日 22:15
  2 件のコメント
Manikanta Aditya
Manikanta Aditya 2024 年 12 月 27 日 15:41
Can you share the entire code file?
Alexandre Da Silva
Alexandre Da Silva 2024 年 12 月 27 日 16:08
Changin the () to {} worked but now it's complaining about the multiplications

サインインしてコメントする。

回答 (2 件)

Star Strider
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.
You can also use the prod function to make this easier —
t = array2table(rand(100, 5))
t = 100x5 table
Var1 Var2 Var3 Var4 Var5 _________ ________ ________ _______ __________ 0.36021 0.78416 0.19064 0.77828 0.33928 0.86373 0.75798 0.06804 0.70605 0.70315 0.13617 0.11643 0.71102 0.38285 0.69832 0.72425 0.28794 0.34274 0.93101 0.28691 0.91087 0.68707 0.10527 0.20181 0.018597 0.25297 0.6527 0.12287 0.56841 0.35464 0.76311 0.6136 0.15964 0.61596 0.95528 0.69494 0.23664 0.67301 0.44096 0.82481 0.0093195 0.74846 0.14387 0.31925 0.41236 0.61274 0.42674 0.32932 0.10132 0.60915 0.34004 0.71392 0.078349 0.7659 0.5931 0.073266 0.33247 0.9104 0.34555 0.72223 0.48368 0.059958 0.75113 0.6843 0.069615 0.59842 0.37785 0.86768 0.97629 0.88959 0.46492 0.063566 0.59707 0.97527 3.9778e-05 0.8089 0.052195 0.85856 0.77135 0.24753
h = linspace(1, 100, 100);
l1 = prod(h-t{2:5,1}) ./ prod(t{1,1}-t{2:5,1})
l1 = 1×100
1.0e+09 * -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0001 -0.0002 -0.0003 -0.0005 -0.0007 -0.0010 -0.0014 -0.0019 -0.0024 -0.0032 -0.0040 -0.0050 -0.0062 -0.0076 -0.0092 -0.0110 -0.0131 -0.0155 -0.0182 -0.0213 -0.0247 -0.0285 -0.0328
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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}))
l1_original = 1×100
1.0e+09 * -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0001 -0.0002 -0.0003 -0.0005 -0.0007 -0.0010 -0.0014 -0.0019 -0.0024 -0.0032 -0.0040 -0.0050 -0.0062 -0.0076 -0.0092 -0.0110 -0.0131 -0.0155 -0.0182 -0.0213 -0.0247 -0.0285 -0.0328
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Check = all(l1 == l1_original) % ‘1’ (true) If All Elements Are Equal
Check = logical
1
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 件のコメント
Alexandre Da Silva
Alexandre Da Silva 2024 年 12 月 27 日 18:33
Thank you so much! You helped a lot!!
Star Strider
Star Strider 2024 年 12 月 27 日 20:03
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

サインインしてコメントする。


Image Analyst
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 件のコメント
Alexandre Da Silva
Alexandre Da Silva 2024 年 12 月 27 日 16:11
Thank you!! Switching the () for {} worked for the subtraction but now it's complaining about the multiplications even after removing the . from .*
this are the files:
Image Analyst
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');
DONE! It worked (or at least it finished)
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 ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by