Same function gives different answers while checking in a command window. why?
Show older comments
I am getting different answers when I want to check the answers of the file " pro.m" in the command window. I have attached my coding and screenshot of the ouput while running the code and output from the command window .Please anyone help me with this.
2 Comments
Guillaume
on 29 Jan 2019
I don't understand why you're surprised. Pass a random input to a function, expect a different output each time unless the function ignore your input.
Also, in your simm_opt.m:
x(1) = x;
y(1) = y;
is absurd. Either x is scalar, and the above is equivalent to x = x, pointless, or x is not scalar and the above will throw an error (trying to assign a non-scalar to a scalar).
"Same function gives different answers while checking in a command window. why?"
Simple: you did not copy the full precison of the data, so your input values are not the same.
In particular, MATLAB's default format (which is short) is to display four decimal places, but this is not the full precision of the data stored in memory. You dutifully copied those values with those four decimal digits only... thus you provided different input data, because what you used as input values does not represent the full prevision of the original data.
Change the format to something with more digits, e.g.:
format long
and then display the values with more precision and copy them again. Even better would be to use exactly the same values by not copy-and-pasting, but by simply referring to the same matrix x0.
EDIT: Note that Rajashree Annapillai has since deleted the uploaded files, making all of the answers totally meaningless as they have no context.
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!