How to convert this equation to MATLAB code?
Show older comments

I am having difficulty writing this long equation into matlab code, please advise
8 Comments
John D'Errico
on 24 Sep 2020
Edited: John D'Errico
on 24 Sep 2020
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.
Very close!
One critical mistake.
- 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
- Are you sure the last constant is 3.5 rather than 35?
- 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
- No need for the outer brackets
- 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.
- No need for these outer brackets, they are confusing: (cos(30))
- 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 , ...
Adam Danz
on 24 Sep 2020
When I change 35 to 3.5 the first 5 outputs are
3.5 3.655 3.7678 3.8386 3.8672
JM
on 24 Sep 2020
Adam Danz
on 24 Sep 2020
Sounds like you figured it out! Socially distant 3-way high-five!
Rik
on 20 Apr 2021
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.
Accepted Answer
More Answers (0)
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!