Matlab graph doesnt look right...not a sinusoid...

Ok so I had to plot a graph that fulfills those conditions in the function.. But when I plot the graph version using fplot(@qtsolver,[0 6*pi]) it plots out a diagonal line with a kink at pi and 2 pi, but shouldnt it print a sinusoid or at least something curvy? I feel like my codes all correct but I dont feel like the graph is :(
function qt = qtsolver(t)
if (0<=t) && (t<pi) qt= 2*t - 0.8*sin(2.5)*t;
else if (pi<=t) && (t<2*pi) qt = 4*pi - 2*t -0.8*sin(2.5)*t-1.6*cos(2.5)*t;
else if (t>= 2*pi) qt = -1.6*cos(2.5)*t;
end
end
end

2 件のコメント

Jan
Jan 2013 年 4 月 10 日
編集済み: Jan 2013 年 4 月 10 日
Of course the graph looks exactly as the code forces it to do. As long as we see the code only, how could we suggest modifications?
Please learn how to format code in the forum. Follow the "? Help" link.
Andrei Bobrov
Andrei Bobrov 2013 年 4 月 10 日
sin(2.5*t) instead of sin(2.5)*t

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

回答 (2 件)

Jan
Jan 2013 年 4 月 10 日
編集済み: Jan 2013 年 4 月 10 日

0 投票

Your function calculates the points separately. But as far as I can see, fplot expects a vector output for a vector input:
function qt = qtsolver(t)
qt = zeros(size(t)); % Pre-allocate
index = (0<=t) && (t<pi);
qt(index) = 2*t(index) - 0.8*sin(2.5)*t(index);
index = (pi<=t) && (t<2*pi);
qt(index) = 4*pi - 2*t(index) -0.8*sin(2.5)*t(index)-1.6*cos(2.5)*t(index);
index = (t>= 2*pi)
qt(index) = -1.6*cos(2.5)*t(index);
[EDITED], t -> t(index)
Andrei Bobrov
Andrei Bobrov 2013 年 4 月 10 日

0 投票

function out = qtsolver(x)
f = {@(t)2*t-.8*sin(2.5*t),...
@(t)4*pi-2*t-0.8*sin(2.5*t)-1.6*cos(2.5*t),...
@(t)-1.6*cos(2.5*t)};
[~,iii] = histc(x,[0 pi 2*pi inf]);
out = arrayfun(@(y,z)y{:}(z),f(iii),x);
end

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

タグ

質問済み:

2013 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by