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

2 ビュー (過去 30 日間)
Jabir Al Fatah
Jabir Al Fatah 2014 年 5 月 30 日
コメント済み: Image Analyst 2014 年 5 月 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 件のコメント
Jabir Al Fatah
Jabir Al Fatah 2014 年 5 月 30 日
please i want the complete answer of my questions step by step.
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 Center および File ExchangeCreating, 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