How to make a vector where each element=5 and 100 elements long?

6 次查看(过去 30 天)
We have a given function: Y1 = 5 – (1/x).
Now (1) I have to use linspace to create an equidistant x-vector from 1 to 100 with 100 elements .
(2) Make a corresponding Y1 vector (use ‘./’ for element-wise division) using the x-vector and the formula for Y1.
(3) Make a corresponding Y2 vector (each element = 5 and 100 elements long ). [It was my main point of the question]
(4) In the same graph, plot Y1 and Y2 against x ! Make title, linecolors, axislabels, tics, grids etc to get a really nice figure. (To determine from the figure, approximately, from which x-value Y2 is a good approximation to Y1?)
Note: I solved them like this but I dont know if they are correct or not.
x=linspace(1,100,100);
y1=5-(1./x);
y2=linspace(1,5,100);
plot(x,y1);
hold on;
plot(x,y2);
hold off;

采纳的回答

Image Analyst
Image Analyst 2014-5-30
If each element of Y2 is supposed to have a value of 5 and be a hundred elements long, you'd want this
Y2 = 5 * ones(1, 100); % or y2 whichever case you want to use.
  2 个评论
Image Analyst
Image Analyst 2014-5-30
But it's your homework, not mine. You did 1 and 2, and I did #3 for you. All you have to do for step 4 is to call title() and xlabel(), etc.
plot(x,y2, 'LineColor', 'r', 'LineWidth', 3);
title('This is my plot', 'FontSize', 30);
ylabel('Y and Y2', 'FontSize', 30);
grid on;
% Function to maximize the window via undocumented Java call.
% Reference: http://undocumentedmatlab.com/blog/minimize-maximize-figure-window
FigurejFrame = get(handle(gcf),'JavaFrame');
FigurejFrame.setMaximized(true);
Oops, I did nearly all of 4 for you. Oh well.

请先登录,再进行评论。

更多回答(1 个)

Mahdi
Mahdi 2014-5-30
What you did produces a vector that starts at 1 and goes up by 5 till it reaches 100 elements. If that was your goal, then it's correct.
But if the question is asking to have a 100 elements which are all equal to 5:
y2=5.*ones((1,100)

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by