How to convert this equation to MATLAB code?

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

8 Comments

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.
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
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.
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 on 24 Sep 2020
Edited: Adam Danz on 24 Sep 2020
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.
Thank you for your help
Sounds like you figured it out! Socially distant 3-way high-five!
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.

Sign in to comment.

 Accepted Answer

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

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Asked:

JM
on 24 Sep 2020

Edited:

JM
on 30 Jun 2021

Community Treasure Hunt

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

Start Hunting!