Plotting for a range of values

조회 수: 2 (최근 30일)
Verdan Chick
Verdan Chick 2020년 12월 10일
답변: Geoff Hayes 2020년 12월 10일
mA = 10;
muA = 0.3;
muB = 0.1;
g = 9.81;
theta = 30;
NA = mA*g*cosd(theta);
NB = mB*g*cosd(theta);
for mB = [1:1:20]
a = ((mA)*(g)*(sind(theta)) - (muA)*(NA) + (mB)*(g)*(sind(theta)) - (muB)*(NB)) / (mA + mB)
end
plot(mB,a)
The graph is not showing a curve and the x-axis values are incorrect. Quite new to matlab, but I have used a for loop in order to gain multiple values for 'a' when mB ranges from 1-20 in increments of 1. Could anyone help?
Thank you

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 12월 10일
Verdan - in your code, the mB and a are scalars whereas you want them to be arrays (for when you plot outside of the loop). Try doing
xData = 1:1:20;
a = size(xData);
for mB = xData
NB = mB*g*cosd(theta); % I moved this here since it depends upon mB
a(mB) = ((mA)*(g)*(sind(theta)) - (muA)*(NA) + (mB)*(g)*(sind(theta)) - (muB)*(NB)) / (mA + mB);
end
plot(xData,a);

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by