Matlab only outputs one variable, and doesn't plot a line.
Show older comments
Trying to plot corner speed vs mass of a vehicle. But when I plot I don't get a line.
%% once this works, add a user defined input for radius
mu = 1; %%Coefficient of friction
r =80; %% the regular line radius is 30m, the racing line is 80m
g = 9.81; %%Gravitational constant
%%m = x;
x = 1000:50:1250 %% car mass for the input roughly 2000-2800 lbs
y = sqrt((mu*(x*g)*r)/x) %% calculates velocity in Meters / second
Where I would expect it to enter the series of x into the equation y= sqrt... , it displays x 1000, 1050 etc in the workspace, but only displays y as 28.0143, which I would expect if it was ONLY calculating the 1000kg value. and not the expected 1000, 1050, 1100, 1150, 1200, 1250 inputted into x of the equation.
5 Comments
Read about the differences between matrix operations (what you used) and array operations (what you should have used):
"Matlab only outputs one variable..."
Actually you only expect one variable (a numeric array): there is only one output variable from either mrdivide or rdivide. Do not confuse the size of one array with having multiple arrays!
Patrick Bradford
on 9 Aug 2019
"So care to share what exactly I did that was in matrix form verse array?"
KALYAN ACHARJYA had already given an answer that showed you exactly what you needed to fix (so there was no point in me repeating that in another answer), but did not explain anything about the fix. Thus I wrote a comment to give you some more information than you got from the answers so far: the documentation that I linked to explains why your code did not return a vector as you expected, which I was hoping you might find useful in future.
"...none of my code works that I made."
Learning how to debug code is part of learning how to write code. There is no easy shortcut, just lots of practice, reading documentation, and learning how to use the debugging tools. A good rule of thumb is to only moving on to writing the next line when you have tested a line of code completey, and checked that it does exactly what you need. You can search this forum for lots of advice on how to debug MATLAB code.
"Nor does any of the methodology from the book I have matlab for engineers"
The best source of documentation on how to use MATLAB is the MATLAB documentation. It is easy to search and is arranged by topic. Note that the online documentation is for the current release: the ultimate reference for your installed version should be the help of your installed MATLAB. It is quite possible that those books use code syntax that has changed on the version that you are using. If you upload the code then we can probably help you to debug it.
Patrick Bradford
on 10 Aug 2019
Edited: Patrick Bradford
on 10 Aug 2019
Stephen23
on 10 Aug 2019
"You said I made it a matrix and not a vector"
Not at all: I have not written anything about what you "made". Here is the full quote of the first sentence of my first comment: "Read about the differences between matrix operations (what you used) and array operations (what you should have used)"
I wrote that you used the wrong type of operator, and I did not mentioned vectors anywhere.
In particular, I wrote that you used a matrix operation, whereas you should have used an array operation. I gave the link to the documentation page that explains the differences: there are five operators which have matrix/array versions, and you used two of them (i.e. * and /). A very short summary of the differences between array operations and matrix operations:
- array operations are element-wise.
- matrix operations are for linear algrebra.
"Considering you are ranked 5th in answers... I find that hard to believe with that reply."
I understand that you are frustrated and are struggling to make your code work, but taking that out on the volunteers who are trying to help you is not appreciated, nor is it likely to help.
Consider also my perspective: when I write a reply to someone, I usually have no idea if they have years of experiences or none, if they can glean information from reading documentation themselves or if they require information to be spoon-fed, etc. I am happy to adapt my advice if they give feedback, but there is no magic way that I can read minds and know exactly what level of advice to offer, or exactly which exact bits they do not understand (use of different terminology often makes that even harder). I have to guess based on the kind of problem they are having or they way they ask... but even these are only proxys at best.
I just noticed that in your code you divide x by x:
sqrt((mu*(x*g)*r)./x) % array division.
% ( ^ ) % vector x
% ^^ % divided (element wise) by
% ^ % vector x
Because mu, r, and g are scalars which multiply to some value k, your code is bascially equivalent to this:
sqrt(k*x./x)
Which, for finite values x./x will return an array of 1's the same size as x:
>> x = 1000:50:1250;
>> x./x
ans =
1 1 1 1 1 1
and so your code is basically doing this:
k * ones(size(x))
What is the exact formula that you are trying to implement ? Does it really divide x by x ?
Answers (1)
KALYAN ACHARJYA
on 9 Aug 2019
Edited: KALYAN ACHARJYA
on 9 Aug 2019
y = sqrt((mu*(x*g)*r)./x) %% calculates velocity in Meters / second
%....................^.........
Now
x =
1000 1050 1100 1150 1200 1250
y =
28.0143 28.0143 28.0143 28.0143 28.0143 28.0143
Please check, why it giving constant value? Plot will be horizantal line.
Categories
Find more on Creating and Concatenating Matrices 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!