How to plot a linear function in using for loop in matlab

15 Ansichten (letzte 30 Tage)
hey guys, thankx in advance for help. i need your help, i have been working on plotting matlab graphs using for loop control structure, when ever i execute my code it is only plotting one dot. the idea is i want plot a strait line
here is my code(script) slope = input('\nEnter the slope:'); y_intercept = input('\nEnter the y_intercept:'); no_of_points = input('\nEnter no_of_points:'); [x,y] = findCoodinates(slope,y_intercept); plot(x,y, 'ko-'); xlabel('x-axis'); ylabel('y-axis'); legend('First Graph'); axis([0 20 -5 30])
here is the (function) function [x,y] = findCoodinates(x,y) for i = 1 x(i) = i; y = x(i)*m + c; end

Akzeptierte Antwort

Star Strider
Star Strider am 5 Sep. 2018
You need to add ‘no_of_points’ as an argument to your ‘findCoodinates’ function. You also need to subscript ‘y’ in it, to return a vector rather than a single value.
slope = input('\nEnter the slope:');
y_intercept = input('\nEnter the y_intercept:');
no_of_points = input('\nEnter no_of_points:');
[x,y] = findCoodinates(slope,y_intercept,no_of_points);
plot(x,y, 'ko-');
xlabel('x-axis');
ylabel('y-axis');
legend('First Graph');
axis([0 20 -5 30])
% here is the (function)
function [x,y] = findCoodinates(m,b,no_of_points)
for i = 1:no_of_points
x(i) = i;
y(i) = x(i)*m + b;
end
end
With those changes, your code worked when I ran it.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by