Matlab strikes again with stupidity
Been using matlab for years and still fighting ridiculous problems
x = [1:.5:10]
y = x.*4;
Z = 4
plot(x,y,'blue'); hold on
plot (x,Z,'red')
Why won't this give me a simple plot with both functions on it. Totally insane. It gives me the x*4 plot but will not give me the constant 4

 採用された回答

Anatoly Kozlov
Anatoly Kozlov 2020 年 4 月 6 日
編集済み: Anatoly Kozlov 2020 年 4 月 6 日

0 投票

x = 0:0.001:1;
c=5;
const = @(x)(c).*x.^(0);
plot(x, const(x))

1 件のコメント

Anatoly Kozlov
Anatoly Kozlov 2020 年 4 月 6 日
編集済み: Anatoly Kozlov 2020 年 4 月 6 日
Note: const = @(x)(c); doesn't work

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

その他の回答 (3 件)

Image Analyst
Image Analyst 2016 年 9 月 17 日

2 投票

Sometime in your years of using MATLAB you probably ran across ones() function but forgot about it. You need to use it so that, for each value of x, you have a value for Z. Here is the correct way to do it.
x = [1 : 0.5 : 10]
y = x .* 4
% Now declare a constant array Z
% with one element for each element of x.
Z = 4 * ones(1, length(x));
plot(x, y, 'b', 'LineWidth', 2);
hold on
plot(x, Z, 'r', 'LineWidth', 2)
grid on;
Otherwise, your Z had only 1 element, not 1 for every value of x so it won't plot a point at every value of x.

5 件のコメント

Robert
Robert 2016 年 9 月 17 日
編集済み: Robert 2016 年 9 月 17 日
right i forgot i need 15 lines of code to make matlab plot a fregin constant YEA! awesome software! And they wonder why people don't want to us matlab anymore and why schools are now teaching mathematica and what you have there is in no way shape or form the correct way to plot a constant
Image Analyst
Image Analyst 2016 年 9 月 17 日
You could do it in fewer lines if you wanted, like combine the two polot() calls, remove comments, etc. I really hate code from people who write compact, cryptic, uncommented code that is impossible to maintain. There is no benefit to code like that over professionally written and documented robust code that is easy to follow. So you save a few keystrokes? Big deal. I'm typing all day long anyway. I'd rather type a few more keystrokes than have anyone who inherits my code hate me, and bug me with endless questions about my poorly written code.
Star Strider
Star Strider 2016 年 9 月 17 日
Plotting the constant is straightforward:
plot(xlim, [Z Z], 'r')
Since xlim is a (1x2) vector, you need only two points to define the ‘Z’ line. This resizes automatically as well.
Robert
Robert 2016 年 9 月 17 日
After further research I have decided for myself the best way to do it is to plot the unit step function with whatever gain you need.
Paul
Paul 2024 年 3 月 2 日
Since R2018b, yline is probably the way to go
x = [1:.5:10];
y = x.*4;
Z = 4;
plot(x,y,'blue'); hold on
%plot (x,Z,'red')
yline(Z,'red')

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

sunny
sunny 2024 年 3 月 2 日

0 投票

x =1:.5:10;
y = x.*4;
Z = 4;
m=5:.5:14;
n=m-x;
plot(x,y,'blue');
hold on
plot (x,n,'red');
hold off;
Sam Chak
Sam Chak 2024 年 3 月 2 日

0 投票

Before I discovered other special non-math functions like ones() and yline(), I used to rely on certain math tricks, such as the sign function, to plot a constant y-value over a specified x range. The concept was to treat plotting as if it were any other vector in a finite-dimensional Euclidean space. However, this trick had a fatal flaw when attempting to plot the constant y-value over , as . Therefore, it was necessary to adjust or shift the 'goalpost' to overcome this limitation.
Example 1: Using the sign function
x = 1:0.5:10;
y1 = 4*x;
y2 = 4*sign(x.^2);
figure(1)
plot(x, y1, 'linewidth', 2), hold on
plot(x, y2, 'linewidth', 2), grid on
xlim([x(1), x(end)])
xlabel x, ylabel y
title('Example 1: Using the sign function')
legend('y_{1}', 'y_{2}', 'fontsize', 16, 'location', 'best')
Example 2: Fatal flaw when crossing
x = -2:0.5:2;
y1 = 4*x;
y2 = 4*sign((x - 0).^2);
figure(2)
plot(x, y1, 'linewidth', 2), hold on
plot(x, y2, 'linewidth', 2), grid on
xlim([x(1), x(end)])
xlabel x, ylabel y
title('Example 2: Fatal flaw when crossing x = 0')
legend('y_{1}', 'y_{2}', 'fontsize', 16, 'location', 'best')
Example 3: Shifting the goalpost
x = -2:0.5:2;
y1 = 4*x;
y2 = 4*sign((x - 2*x(1)).^2);
figure(3)
plot(x, y1, 'linewidth', 2), hold on
plot(x, y2, 'linewidth', 2), grid on
xlim([x(1), x(end)])
xlabel x, ylabel y
title('Example 3: Shifting the goalpost')
legend('y_{1}', 'y_{2}', 'fontsize', 16, 'location', 'best')

カテゴリ

タグ

質問済み:

2016 年 9 月 17 日

回答済み:

2024 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by