Why am I getting invalid expression?

1 回表示 (過去 30 日間)
Gina Jarid
Gina Jarid 2020 年 10 月 1 日
編集済み: John D'Errico 2020 年 10 月 1 日
clear; clc
h = 34.869 ;
c = 13.679 ;
J2eV = 0.845 ;
counter = 0 ;
for lambda = 800:6:854
counter = counter + 1 ;
Lambda(counter) = lambda ;
E(counter) = ((h * c) / lambda) / J2eV ;
if ((E > 0.673 && (E < 0.678))
disp('Energy in eV')
end
end
E
Lambda

採用された回答

drummer
drummer 2020 年 10 月 1 日
編集済み: John D'Errico 2020 年 10 月 1 日
You should declare Lambda and E as you made for counter.
You also had some flaws in the if condition (a missing parenthesis, and declare the indexes to E)
I made a quick review on the code, and I did not check whether it is optimized or not.
It follows below. =)
Cheers
clear; clc
h = 34.869 ;
c = 13.679 ;
J2eV = 0.845 ;
counter = 0 ;
Lambda = zeros; % declare as zeros
E = zeros; % declare as zeros
for lambda = 800:6:854
counter = counter + 1 ;
Lambda(counter) = lambda ;
E(counter) = ((h * c) / lambda) / J2eV ;
if ((E(counter) > 0.673) && (E(counter) < 0.678)) % missing parenthesis and insert indexes to E
disp('Energy in eV')
end
end
E
Lambda
  1 件のコメント
madhan ravi
madhan ravi 2020 年 10 月 1 日
編集済み: madhan ravi 2020 年 10 月 1 日
What? How’s this different from my answer. By the way E and Lambda are not preallocated properly.
zeros would create a scalar not a vector

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 10 月 1 日
編集済み: madhan ravi 2020 年 10 月 1 日
h = 34.869 ;
c = 13.679 ;
J2eV = 0.845 ;
lambda = 800:6:854;
E = ((h * c) ./ lambda) / J2eV ;
compose("Energy in eV: %f", E((E > 0.673) & (E < 0.678)))
  1 件のコメント
madhan ravi
madhan ravi 2020 年 10 月 1 日
h = 34.869 ;
c = 13.679 ;
J2eV = 0.845 ;
counter = 0 ;
[Lambda, E] = deal(zeros(800:6:854));
for lambda = 800:6:854
counter = counter + 1 ;
Lambda(counter) = lambda ;
E(counter) = ((h * c) / lambda) / J2eV ;
if ((E(counter) > 0.673) && (E(counter) < 0.678))
disp('Energy in eV')
end
end
E
Lambda

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by