Create an 1D array x from 0 to 10 interval size 0.5

49 views (last 30 days)
Create an array x from 0 to 10 with an interval size 0.5.wtite code to compute the value for the function y=x^2-10

Answers (2)

Cris LaPierre
Cris LaPierre on 12 Sep 2022
See MATLAB Onramp, Ch 4 covers creating vectors and matrices.

Kamran Yunus
Kamran Yunus on 1 Nov 2022 at 11:45
%1D Array Creation & Plotting
x = [0:0.5:10]'; %x value is given from 0 to 10 with interval of 0.5. I have taken transpose here to simplify the matrix multiplication in next step
y = (x*x')-10; %without using dot operator. To satisfy the matrix multiplication method x' is taken for multiplication with x
ydot = x.^2-10; %with dot operator. each x value is squared to get the ydot value
x0 = 0; % first x value is 0
y0 = (x0*x0')-10; %using first x value we calculate first y value
x1 = 10; %last x value is 10
y1 = (x1*x1')-10; %using the last x value we calculate last y value
x_start = 0; % x start value is given 0
ydot_start = (x_start.^2)-10; %ydot start value is calculated using x start value
x_end = 10; %x end value is given 10
ydot_end = (x_end.^2)-10; %ydot end value is calculated using x end value
plot(x,y,'linewidth',1,'color','k') %plotting x&y here, our x &y are matrices, linewidth taken is 1 with color black
hold on %we use hold on while plotting to add another curve/line to the graph
plot(x,ydot,'linewidth',1,'color','k'); %plotting x&y here, y is ydot here, linewidth taken is 1 with color black
grid on; %it makes the graph grid on
xlabel('x axis') %it indicates the label of xaxis
ylabel('y axis') %it indicates the label of yaxis
axis([0 10 -10 100]) %axis values are taken in respect all the x,y & ydot values
title('1D Array creation & Plotting') %it gives the title of the graph
legend('x,y','x,ydot') %legend creates a legend with descriptive labels for each plotted data
  1 Comment
John D'Errico
John D'Errico on 5 Nov 2022 at 9:41
Please don't do obvious homework assignments for students. This does not help the student, only teaching them that someone is willing to do their work for them as soon as they ask. It does not help the site, because it teaches the student to continue posting their homework on the forum with no effort made.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by