Quick question. Am i doing this right?
4 views (last 30 days)
Show older comments
Hey guys,
so I'm in my first year and first week of engineering and messing around doing some questions we've been given to try out.
so, I'm doing ok so far. Hit and miss but generally I can find a way to get the right answer. Not sure why I'm getting this answer for this question though. Please help.
Q: Define variables X and Y as X = 5.1 and Y = 4.2 and evaluate, 3/4 xy - 7x/y^2 + sqrt(xy)
(The / are fractions rather than a divided by) The following is my answer
A: X =5.1; Y=4.2;
sym(3/4) * x * y - sym(7 * x / y^2) + sqrt(x * y)
When i do that, the result i get is not straightforward. Rather it is, (3*2^(1/2)*119^(1/2))/10 + 58973/4200
not sure where I'm going wrong. Thanks in advance
0 Comments
Accepted Answer
David Young
on 2 Mar 2015
Ordinary evaluation of an expression doesn't require sym. Simply
(3/4) * x * y - 7 * x / y^2 + sqrt(x * y)
will evaluate the expression to give you a single number. Normally you would assign this to a variable, for example:
z = (3/4) * x * y - 7 * x / y^2 + sqrt(x * y)
Note, though, that variable names are case-sensitive, so you have to be consistent. Either use upper case X and Y or lower-case x and y for both the assignments and the expression.
0 Comments
More Answers (2)
the cyclist
on 2 Mar 2015
Edited: the cyclist
on 2 Mar 2015
Don't use those sym commands. The expression will be evaluated numerically, which it seems to me is what is being asked for.
(3/4) * x * y - (7 * x / y^2) + sqrt(x * y)
0 Comments
Emre Mutlu
on 2 Mar 2015
2 Comments
Stephen23
on 2 Mar 2015
Edited: Stephen23
on 12 Mar 2015
If you don't understand the difference between numeric and symbolic calculations then you should revise this, otherwise using MATLAB will always be a struggle. There is plenty of information on the internet. One of the core points to realize is that often it is not possible to generate exact symbolic solutions to a problem, not matter how much we would like to do this. And in some of these cases numeric solutions can be easier to calculate.
So the answer to your question is: they are different tools, and like selecting any tools for a job, the one you choose depends on the problem, what the tools can do, and the desired solution.
Random video that "I was watching a clip where someone..." is not going to introduce you to good programming practices, especially when MATLAB themselves have many excellent tutorials and videos:
John D'Errico
on 12 Mar 2015
Edited: John D'Errico
on 12 Mar 2015
Symbolic computations will be FAR slower than normal computations with numeric values. They will consume far more memory.
Whoever gave you that video "advice" to always use symbolic coefficients should be shot at dawn. I hope that you merely misunderstood what they were saying, as that is simply terrible advice.
In most cases, a symbolic computation will be wild overkill. The major cost of using the normal double computations is the fraction, when converted to a double, will be a double precision number. And MOST such double precision numbers will not be represented exactly. For example, you cannot represent the fraction
2 / 3
as a double in exact form. Instead, there will be an error in the least significant bit, because a binary representation cannot exactly reproduce the desired number.
That simply means you need to take some care when working with double precision numbers. Learn to use them. Learn to use tolerances. However, the person who gave you that advice has chosen to fear such numbers rather than bothering to learn to work with them properly and intelligently. This is, I am sorry to say, a foolish thing.
Perhaps we need the proper analogy to make this point, so consider this little fable...
I have a small shop where I like to make things. Suppose one day some fool, his name might be Oliver Hardy (just call him Ollie for short) needs to drive in a screw into some wood. He sneaks into my shop, and therein finds many wondrous tools - a hammer, saw, chisels, etc. Ollie first bangs his thumb with the hammer, then cuts off the tip of his finger with a saw. He perseveres, then using the chisel, nicks an artery. Just a small nick, so still alive for now, he finally finds a screwdriver and accomplishes his task. Ollie is so proud of his achievement that he now posts YouTube videos, advising all who watch to never use any tool but a screwdriver.
Of course, you know where this little fable ends up. Ollie has a friend named Stan (last name Laurel) who needs to cut a small piece of wood in half. Stan takes the advice, and with his trusty screwdriver, starts to chop into the wood. Eventually, after poking himself in the eye several times and suffering only two minor heart attacks (Stan is rather old by now), Stan does sever the wood roughly in half.
The moral is...
1. Never work with anything harder than Playdough.
2. Never take the advice of Ollie.
3. Learn to properly use all of the tools at your command. This means knowing which tool will be appropriate for any job. It means knowing how to use that tool.
I need to go now. Ollie is still bleeding, and the paramedics are here to pick up Stan.
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!