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