フィルターのクリア

Why am I having so many errors?

2 ビュー (過去 30 日間)
Shawn Simon
Shawn Simon 2015 年 10 月 23 日
コメント済み: Walter Roberson 2015 年 10 月 24 日
The purpose of the code is to take the inputs and calculate the weight of something on a different planet. Here is the code:
name = input('What is the name of the exoplanet? ', 's');
massplanet = input( sprintf('What is the estimated mass of %s(kg)? ', name), 's');
radius = input( sprintf('What is the estimated radius of %s(km)? ', name), 's');
massspecimen = input('What is the mass of the specimen (kg)? ');
%calculating acceleration due to gravity of other planet using:
%G * mass / radius^2
%G is a constant
Accel_of_G_exo = ((6.67e-11) * massplanet) / radius^2 ;
%calculating weight of specimen using acceleration due to gravity of other
%planet and mass of specimen
Weight_exo = Accel_of_G_exo * massspecimen;
%trying to print the name of the planet as well as the variable Weight_exo
fprintf('On %s, it would weigh about %f Newtons.',name, Weight_exo);
However, when I run the code, I am getting an error on the line where I try to define the variable 'Accel_of_G_exo'. I am assuming the variables 'massplanet' and 'radius' are stored as string types, therefore they can not be incorporated in an equation. Also, on the last line, I tried to print a statement that included the variables 'name' and 'Weight_exo', however, I do not think that I can use %s with fprintf. Thank you so much for the help. I am new to MATLAB and am still learning the program.

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 24 日
You need to str2double() the strings that hold the masses. Or just skip the 's' option of input() to read the values as numeric.
  1 件のコメント
Shawn Simon
Shawn Simon 2015 年 10 月 24 日
Thank you!

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

その他の回答 (1 件)

Stalin Samuel
Stalin Samuel 2015 年 10 月 24 日
Accel_of_G_exo = ((6.67e-11) * massplanet) / radius.^2 ;
Weight_exo = Accel_of_G_exo * massspecimen ;
fprintf('On %s, it would weigh about %d Newtons.',name, Weight_exo);
  2 件のコメント
Shawn Simon
Shawn Simon 2015 年 10 月 24 日
When I ran this, it gave me an error on the Accel_of_G_exo line saying there was an error using '/'.
Walter Roberson
Walter Roberson 2015 年 10 月 24 日
That would happen because your radius is a string, which in MATLAB is a vector of characters.

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

カテゴリ

Help Center および File ExchangeGravitation, Cosmology & Astrophysics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by