The code has no errors but won't show a plot, what am i doing wrong?
clear all, close all, clc
diffmax=100
x=0;0.1;diffmax
plot(f(x))
hold on
xlabel('x')
ylabel('velocity')
axis([0 diffmax 0 30])
function velocity=f(x)
if x>75
velocity = 25;
else
velocity = x/3;
end
end

1 件のコメント

Clara Holmén
Clara Holmén 2020 年 12 月 2 日
I have now changed the code bc the slope that was plotted was wrong. I created a matrix y to plot against x, instead of calling the function in plot(f(x). However the plot then disappeared again..
I have colons for x this time, have i missed something else?
clear all, close all, clc
matris=[]
diffmax=100
x=0:diffmax
for i=1:diffmax
y=f(x(i))
matris=[matris;y]
end
plot(x, y)
hold on
xlabel('x')
ylabel('velocity')
axis([0 diffmax 0 30])
function velocity=f(x)
if x>75
velocity = 25;
disp('rad 18')
else
velocity = x/3;
disp('rad 21')
end
end

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

回答 (2 件)

KSSV
KSSV 2020 年 12 月 2 日

0 投票

You have not defined f in your code...you need to define f and then call plot...something like this:
diffmax=100
x=0:0.1:diffmax ;
f = @(x) sin(x)
plot(x,f(x))

4 件のコメント

Clara Holmén
Clara Holmén 2020 年 12 月 2 日
Thank you for the quick answer!
So it is not enough to have f as a function of x and have x defined??
KSSV
KSSV 2020 年 12 月 2 日
If you have defined an anonymous function (like in the answer), then you have to substitue it to get the function values for the given inputs.
Clara Holmén
Clara Holmén 2020 年 12 月 2 日
ok ill try it!
KSSV
KSSV 2020 年 12 月 2 日
clear all, close all, clc
diffmax=100
diffmax = 100
x=0:0.1:diffmax
x = 1×1001
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
plot(f(x))
hold on
xlabel('x')
ylabel('velocity')
axis([0 diffmax 0 30])
function velocity=f(x)
if x>75
velocity = 25;
else
velocity = x/3;
end
end

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

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 2 日
編集済み: KALYAN ACHARJYA 2020 年 12 月 2 日

0 投票

Here is the issue, the rest code is perfectly OK
x=0:0.1:diffmax
%..^ ..^ not semicolon, its colon

7 件のコメント

KSSV
KSSV 2020 年 12 月 2 日
Note that there is no f defined in the code.
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 2 日
編集済み: KALYAN ACHARJYA 2020 年 12 月 2 日
Sir, I dont think it is required, as f as function call within plot command. Can you share more please? Thanks
Clara Holmén
Clara Holmén 2020 年 12 月 2 日
i have semicolons in my code, this snippet is from the answer from KSSV!
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 2 日
編集済み: KALYAN ACHARJYA 2020 年 12 月 2 日
@Clara Replace with colon in your original code, I have tried to answer this "
Figure but no plot
KSSV
KSSV 2020 年 12 月 2 日
Ohh yes, the function f is defined at the bottom. Just now I am having a look...I have copied only the first few lines..as the problem was in first line.
Clara Holmén
Clara Holmén 2020 年 12 月 2 日
Sorry i misunderstood. It worked, thank you very much!!
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 2 日
Hope your problem is resolved anyway.
Happy :)

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2020 年 12 月 2 日

コメント済み:

2020 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by