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

106 ビュー (過去 30 日間)
kanamani
kanamani 2022 年 9 月 12 日
コメント済み: John D'Errico 2022 年 11 月 5 日
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

回答 (2 件)

Kamran Yunus
Kamran Yunus 2022 年 11 月 1 日
%1D Array Creation & Plotting
clear;
clc;
%Inputs
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 件のコメント
John D'Errico
John D'Errico 2022 年 11 月 5 日
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.

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


Cris LaPierre
Cris LaPierre 2022 年 9 月 12 日
See MATLAB Onramp, Ch 4 covers creating vectors and matrices.

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by