How can i plot this graph?

1 回表示 (過去 30 日間)
Farooq Hussain
Farooq Hussain 2020 年 4 月 12 日
コメント済み: Ameer Hamza 2020 年 4 月 13 日
for x=0:0.5:5;
if 0<x<2
y=((1+0.5*(1-cos(2*pi*x/5))).^-0.5);
else x>2;
y=1;
end
end
plot(x,y)

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 12 日
編集済み: Ameer Hamza 2020 年 4 月 12 日
You can use this vectorized version which is usually faster in MATLAB as compared to for loop.
x = 0:0.1:5;
y = (x<2)*((1+0.5*(1-cos(2*pi*x/5))).^-0.5) + (x>=2)*1;
plot(x,y)
  12 件のコメント
Farooq Hussain
Farooq Hussain 2020 年 4 月 13 日
Assalam o alaikum!
Dear Ameer Hamaz...Hope you are doing well...Please replace the value of A(x)
in my code u*A(x)......when
L=10;
a=0;
h=0.5;
n=20;
x=a:h:n;
f1=@(x,u) u * A(x);
%%% Initial Conditions %%%
u(1)=1;
for i=1:length(x)-1;
m1=h*f1(x(i), u(i));
m2=h*f1(x(i)+0.5*h, u(i)+0.5*m1);
m3=h*f1(x(i)+0.5*h, u(i)+0.5*m2);
m4=h*f1(x(i)+h, u(i)+m3);
u(i+1)=u(i)+(1/6)*(m1+2*m2+2*m3+m4);
end
plot(x,u,'k')
Ameer Hamza
Ameer Hamza 2020 年 4 月 13 日
You can define it like this
A = @(x) (0<x & x<L).*(1+0.5*(1-cos(2*pi*x/L))-1/2) + (L<x).*1;
f1=@(x,u) u * A(x);

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

その他の回答 (1 件)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 4 月 12 日
編集済み: Thiago Henrique Gomes Lobato 2020 年 4 月 12 日
Making y=... substitute the whole array. Try something like this:
x=0:0.5:5;
y = zeros(size(x));
for idx = 1:length(x)
if 0<x(idx) && x(idx)<2
y(idx)=((1+0.5*(1-cos(2*pi*x(idx)/5))).^-0.5);
else x(idx)>2;
y(idx)=1;
end
end
plot(x,y)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by