plot function f(x)

Referring to figure below, plot the function f(x) in the domain −2 ≤ x ≤ 5 using Method 1 and Method 2.
a) Method 1: write a program in a script file using conditional statements and loops
b) Method 2: create a user-defined function for f(x) and then use the function in a script file to make the plot.

3 件のコメント

darova
darova 2020 年 5 月 21 日
Can you please show something? Do you have any ideas?
xavier
xavier 2020 年 5 月 21 日
編集済み: xavier 2020 年 5 月 21 日
method 1
x=[-2:5];
n=input('Enter the n value');
for k=1:n
S(k)=(-1)^k*k/2^k;
if x<=-1
y=20;
elseif x<=-1 & x<=1
y=(-5*x)+5;
elseif x<=1 & x<=3
y=(-10*x.^2)+(35*x)-21
elseif x<=3 & x<=4
y=(-5*x)+11;
elseif x>=4
y=-10;
end
end
xavier
xavier 2020 年 5 月 21 日
for the method 2
%PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Using
x=[-2:5];
y=20;
yd=-5*x+5;
ydd=(-10*x.^2)+35*x-21;
yddd=(-5*x)+11;
ydddd=-10;
plot(x,y,'--b',x,yd,'-c',x,ydd,'--y',x,yddd,'-g',x,ydddd,'--k')

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

回答 (2 件)

darova
darova 2020 年 5 月 21 日

0 投票

Here are some suggestions for method 1

6 件のコメント

xavier
xavier 2020 年 5 月 21 日
it is like this ? then what value must i key in ?
darova
darova 2020 年 5 月 21 日
You made some mistakes, look
xavier
xavier 2020 年 5 月 21 日
編集済み: xavier 2020 年 5 月 21 日
so what should i write at the command window
Walter Roberson
Walter Roberson 2020 年 5 月 21 日
your else are wrong. not x(k)<=-1 and instead x(k)>-1
xavier
xavier 2020 年 5 月 21 日
編集済み: xavier 2020 年 5 月 21 日
sorry for asking walter, may i know which row do you refer ?
it is like this walter ?
darova
darova 2020 年 5 月 21 日
YOu can simply your if...else conditions
x = -2:0.1:5; % note the dot '0.1', not '0,2'
for k = 1:length(x)
if x(k) < -1
% condition 1
elseif x(k) < 1
% condition 2
elseif x(k) < 3
% condition 3
elseif x(k) < 4
% condition 4
else
% condition 5
end
end
plot(x,y)

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

Sasika
Sasika 2023 年 6 月 28 日

0 投票

f(x)=x^2+5*x-6 -8<x<3

1 件のコメント

Walter Roberson
Walter Roberson 2023 年 6 月 28 日
That code will fail for non-scalar x, or for x numeric that is not either positive integer or else logical true or false. And it likely gives u desirable answers in most cases.
Please do some testing of your code before posting your solutions as Answer to a Question.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2020 年 5 月 21 日

コメント済み:

2023 年 6 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by