# 3.2 Write a MATLAB script that plots a straight line: y = mx + c. The script first prompts for the slope m, the y-intercept c, and a range of x values (beginning and ending values). Then the script plots the line using the entered information

138 views (last 30 days)

Show older comments

### Answers (1)

Akinrinmade Daniel
on 23 Apr 2022

Edited: DGM
on 9 Oct 2024

function StraightLine = straight_line(m,x1,x2,c)

%Summary

% Write a MATLAB script that plots a straight line y = mx + c.The script

% first prompt for the slope m, the y-iintercept c, and a range of x values

% (beginning and ending values). Then the script plots the line using the

% entered information.

% Akinrinmade Daniel

m = input('Enter the slope of the graph: ');

c = input('Enter the y-intercept c of the graph: ');

x1 = input('Enter x1 value: ');

x2 = input('Enter x2 value: ');

input('The incremented value of x is assumed to be 1, press enter')

x = [x1:x2];

SraightLine = m*x + c; y = SraightLine;

x,y

plot(x,y);

xlabel('x-axis');

ylabel('y-axis');

title('A Sraight line gragh');

axis equal;grid on

end

##### 3 Comments

DGM
on 9 Oct 2024

SraightLine = m*x + c;

y = SraightLine;

Also, what's the point of this variable juggling?

I guess it's fair enough to tempt fools to copy homework laced with subtle(ish) flaws.

Sam Chak
on 9 Oct 2024

If the homework specifically asks for a script, it's best to follow that instruction and use a script file. Misleading the OP (@Taysir Alzoubi) in using a function instead of a script might lead to points being deducted since it doesn't fulfill the assignment requirements.

If you want to use functions for your own practice or if it enhances your solution, you can do so in your own projects. If you're ever unsure, it's a good idea to ask the forumers in MATLAB Answers for clarification.

### See Also

### Community Treasure Hunt

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

Start Hunting!