Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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 commentaires
tomer kapuri
tomer kapuri le 24 Nov 2018
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 le 24 Nov 2018
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?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by