Write an M file to evaluate the equation yx x26x5 for all v
Write an M- file to evaluate the equation y(x) = x^2-6x+5 for all values of x between -1 and 3, in steps of 0.1. Use a for-loop to define x and y and plot the resulting function using a 3-point-thick dashed red line, Use vectorization to define x and y and plot the resulting function using a 5-point-thick solid green line.
Solution
Ans:
clc;
clear all;
close all;
%%% for loop for defining and x and y %%%%%%%%%%%%
for i=1:1:41
x(i)=(i-11)/10;
y(i)=x(i)^2-6*x(i)+5;
end
figure
plot(x,y,\'--r\',\'LineWidth\',3)
xlabel(\'x\')
ylabel(\'y\')
title(\'for loop for defining and x and y \')
%%%%% vectorization for x and y %%%%
x=-1:0.1:3;
y=x.^2-6.*x+5;
figure
plot(x,y,\'-g\',\'LineWidth\',5)
xlabel(\'x\')
ylabel(\'y\')
title(\'vectorization for defining and x and y \')
