I need to calculate M but I have a and e_ and I want to put them inside equation at the same time. When I write double for loop and press run I get to much answers. If I have only one 'for' then I get 4 answers as expected, but with two for loops I think I get double answers. Is using double for loop more complicated as I am doing or what is the problem here
for a = [6378137.000,6378137.000,6377397.155,6378136.000]
for e_ = [0.0818191910428151,0.0818191908426216,0.0818191908426216,0.0818191065283643]
M = (a *(1-(e_)^2)) / (sqrt(1-(e_)^2) * (sin(fi))^2)^3
end
end

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 20 日

0 投票

You don't need to use a double for loop here, as it goes through all possible combinations of elements of a and e_ (a(1) & e(1), a(1) & e(2), a(1) & e(3), and so on, total 16 combinations), which is not what you want.
A double for loop is not complicated but it is not required here. A single for loop is what you need.
Additionally, a simpler approach is to vectorize the code -
a = [6378137.000,6378137.000,6377397.155,6378136.000];
e_ = [0.0818191910428151,0.0818191908426216,0.0818191908426216,0.0818191065283643];
%Random value
fi = pi/3;
%Change the display format
format long g
M = (a.*(1-(e_).^2)) ./ (sqrt(1-(e_).^2) .* (sin(fi))^2).^3
M = 1×4
1.0e+00 * 15169407.1131592 15169407.112909 15167647.5066156 15169404.6292128
Note the use of element-wise operators .*, ./ and .^
For more info, refer to - Vectorization, Array vs Matrix Operations

1 件のコメント

David
David 2023 年 12 月 20 日
編集済み: David 2023 年 12 月 20 日
yeah you are right, I over complicated . thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2023 年 12 月 20 日

編集済み:

2023 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by