fprintf linspace and variable IN THE RIGHT ORDER

3 ビュー (過去 30 日間)
Daniel Peterson
Daniel Peterson 2018 年 10 月 13 日
回答済み: Stephen23 2018 年 10 月 14 日
My code is printing out the array in 5 consecutive variables (xx, xx, xy, yy, yy) even though it's meant to print x y, x y, x y, x y, x y. Please help lmao
clc
clear
m = input('Slope: ');
c = input('Intercepts: ');
p = input('How many elements between 0 and 10: ');
x = linspace(0,10,p);
y = (m*x)+c;
l = 0;
for l = p
l = l+1;
fprintf('The y value when x equals %0.1d is: %0.1f\r\n', x, y)
end

回答 (2 件)

jonas
jonas 2018 年 10 月 13 日
編集済み: jonas 2018 年 10 月 13 日
Some problems with your loop.
clc
clear
m = input('Slope: ');
c = input('Intercepts: ');
p = input('How many elements between 0 and 10: ');
x = linspace(0,10,p);
y = (m*x)+c;
l = 0;
for l = 1:p
fprintf('The y value when x equals %0.1d is: %0.1f\r\n', x(l), y(l))
end

Stephen23
Stephen23 2018 年 10 月 14 日
Get rid of the loop entirely, you don't need it:
x = linspace(0,10,p);
y = (m*x)+c;
fprintf('The y value when x equals %0.1d is: %0.1f\n', [x,y].')

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by