MATLAB Answers

Matlab only outputs one variable, and doesn't plot a line.

2 views (last 30 days)
Patrick Bradford
Patrick Bradford on 9 Aug 2019
Commented: Stephen Cobeldick on 10 Aug 2019
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.


Show 2 older comments
Stephen Cobeldick
Stephen Cobeldick 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
Patrick Bradford on 10 Aug 2019
After Mr ACHARJYA pointed out WHAT I made the mistake on. It was clear. You said I made it a matrix and not a vector, then refrenced a farely wide scope page. Matrix division and array division would have been a little bit less vague hint without showing me how. I thought your comment was in addition to what he said, so I spent another 30 min trying to figure out what I did. In this case I am still stuck on why it outputs velocity of 28.0143. Which is the velocity evaluated at ONLY 1000 kg, where I would think it would evalutate y for each x input. Do you have a suggestion for a single operation I need to research? Keep in mind this isn't for school or work, this is just me playing with matlab to brush up on my skills so I can use the tools as needed later during undergrad.
FYI I don't know if it's just my version, but simple operations that used to work like:
x = 0:pi/100:2*pi;
y1 = cos(x*4);
plot (x,y1)
Simple code that should work. runs first and second lines, but when I delete comment in front of plot, it faults on both line 2 and 3. Not exactly logical. and makes me miss python.
Stephen Cobeldick
Stephen Cobeldick 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:
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 ?

Sign in to comment.

Answers (1)

Edited: KALYAN ACHARJYA on 9 Aug 2019
y = sqrt((mu*(x*g)*r)./x) %% calculates velocity in Meters / second
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.


Sign in to comment.

Sign in to answer this question.