need to create a script m-file that uses a for loop to plot the equation of a line (y = m*x + b)

조회 수: 2 (최근 30일)
How i do this?
Part a:
In this two-part exercise, you need to create a script m-file that uses a for loop to plot the equation
of a line (y = m*x + b) divided by the iteration variable i. For part a, your code should
1. Create a vector x that ranges from -10 to 10 by 0.2.
2. Create a vector y that is the equation of a line with slope m=3 and y-intercept b=0.
3. Use a for loop to loop through the stored x-values and create a variable ymod that is the i-th y-
value divided by the counter variable i.
4. Plot ymod as a function of x with a black line between points and red circles at the points
Include axes labels and a title too
5. Contain comments on each line of the code explaining what each line does.
Part b:
1. Using conditional statements, do the following:
ymod = y(i)/i for values of y(i) less than 0
ymod = y for values of y(i) greater than or equal to 0 and less than 12
ymod = 12 for all other values of y(i)
2. Plot ymod as a function of x with a black dashed line between points and blue asterisks at the
points. Include axes labels and a title again.
3. Again comment each line of your code, to explain what is going on.
For this exercise, please submit a copy of the code for both section a and b, as well as a
printout of each plot generated with your code.
  댓글 수: 8
tomer kapuri
tomer kapuri 2018년 11월 24일
until now i write this:
m =3;
b = 0;
x=linspace(-10,10,0.2);
y = m*x + b;
plot(x,y,);
grid on;
and now i don't understand what i need to do...
"3. Use a for loop to loop through the stored x-values and create a variable ymod that is the i-th y-
value divided by the counter variable i."
dpb
dpb 2018년 11월 24일
OK, that's a start...
Check your work as you go, however...what's the result of
x=linspace(-10,10,0.2);
? linspace is more suited when you know how many elements you want; what's another way you've learned (or the tutorial showed) for creating a vector at a given spacing between bounds?
Did you actually get a plot from the above? (If so, you did something else besides just what is shown).
For 3., what does doc for show for examples to use a loop? Doesn't the very first one show addressing the elements of a (2D) array?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by