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

97 ビュー (過去 30 日間)
Taysir Alzoubi
Taysir Alzoubi 2018 年 11 月 10 日
コメント済み: Sam Chak 2024 年 10 月 9 日
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. (10 points)

回答 (1 件)

Akinrinmade Daniel
Akinrinmade Daniel 2022 年 4 月 23 日
編集済み: DGM 2024 年 10 月 9 日
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 件のコメント
DGM
DGM 2024 年 10 月 9 日
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
Sam Chak 2024 年 10 月 9 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by