How to convert this equation to MATLAB code?

I am having difficulty writing this long equation into matlab code, please advise

8 件のコメント

John D'Errico
John D'Errico 2020 年 9 月 24 日
編集済み: John D'Errico 2020 年 9 月 24 日
While need not be explicitly homework, it is sure;y part of some project, assigned to you. So why not try something? You might learn something along the ay, and then someone can show you how to fix it.
I'll give you a hint, in that since you have written tan(30), you probably need to know how to use the tan and cos functions that work with degrees. Thus
help tand
help cosd
So TRY IT! If you absolutely have no clue how to use MATLAB, then you need to read the manual. Better yet, try the MATLAB OnRamp tutorial.
JM
JM 2020 年 9 月 24 日
編集済み: Adam Danz 2020 年 9 月 24 日
x1=linspace(0,60.68,200);
y1=(x1*tan(30)-(1/2)*(((x1.^2)*9.81)/25*(cos(30)))+3.5);
This is the code I have tried and it does not graph the correct function
Adam Danz
Adam Danz 2020 年 9 月 24 日
編集済み: Adam Danz 2020 年 9 月 24 日
Very close!
One critical mistake.
  1. This section is wrong. You're doing A/B*C but that's not how that part of the equation is defined.
(((x1.^2)*9.81)/25*(cos(30)))
% -------A------ B ----C----
Two cautionary comments
  1. Are you sure the last constant is 3.5 rather than 35?
  2. The "cos(30)" and "tan(30)" in the equation is likely in reference to degrees so you'll either need to convert 30deg to radians (see deg2rad) or use cosd() & tand() which receives degree inputs.
Some very minor points
  1. No need for the outer brackets
  2. The brackets here are unnecessary but if they help you, that's totally fine: (x1.^2)*9.81; You can review Matlab's Operator Precedence.
  3. No need for these outer brackets, they are confusing: (cos(30))
  4. There's no need for the outer brackets aound the fraction that contains parentheses in the image you shared but, as with point #2, sometimes those brackets are helpful for perceptual grouping and I would probably keep them.
Result
After making those changes, including the interpretation of degrees and that the constant at the end is 35, the first few elements of the row-vector outptut are
y1 = [ 35 35.155 35.268 35.339 , ...
*Posted before seeing John D'Errico's answer.
Adam Danz
Adam Danz 2020 年 9 月 24 日
When I change 35 to 3.5 the first 5 outputs are
3.5 3.655 3.7678 3.8386 3.8672
Adam Danz
Adam Danz 2020 年 9 月 24 日
編集済み: Adam Danz 2020 年 9 月 24 日
You haven't implemented my "critical mistake" #1 or John's 1/2*3 advice (same advice, two different people). The demonimator is still incorrect.
JM
JM 2020 年 9 月 24 日
Thank you for your help
Adam Danz
Adam Danz 2020 年 9 月 24 日
Sounds like you figured it out! Socially distant 3-way high-five!
Rik
Rik 2021 年 4 月 20 日
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

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

 採用された回答

John D'Errico
John D'Errico 2020 年 9 月 24 日
編集済み: John D'Errico 2020 年 9 月 24 日

0 投票

Well, you did make an effort, and it was pretty close. But did you read my response?
Did you read the help for tand and cosd? Why not? There was a reason I wrote that hint. They are different from tan and cos. tan and cos work in radians. But you seem to be using DEGREES. So you NEED to use cosd and tand.
Next, you MULTIPLIED by cos(30). While you may think that
1/2*3
ans =
1.5
should yield 1/6 as a result, it does not. If you want to divide by the product of two numbers, you need them in parens. So something like this
1/(2*3)
ans =
0.16667

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

質問済み:

JM
2020 年 9 月 24 日

編集済み:

JM
2021 年 6 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by