How can i plot this graph?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2020년 4월 12일
편집: Ameer Hamza
2020년 4월 12일
0 개 추천
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
2020년 4월 12일
Thank you very much for your dear Ameer ....Will this vectorized version work correctly if i use (x<2)*((1+0.5*(1-cos(2*pi*x/5))).^-0.5) + (x>=2)*1; by using RK method? As i use this expression but, i don not get the required graph..Any suggestion please
Ameer Hamza
2020년 4월 12일
Can you paste your code here?
Farooq Hussain
2020년 4월 12일
편집: Farooq Hussain
2020년 4월 12일
%%% Range-Kutta method %%%%%
L==8;
CM=-1;
a=0;
h=0.5;
n=20;
x=a:h:n;
%%% Functions %%%
%%% A(x) =(1).*((x < 0)) + ((1-0.5*CM*(1-cos(2*pi*x/L))).^-0.5).*((0<= x) & (x <=L))+(1).*((x >L));
f1=@(x,u) u* ((1).*((x < 0)) +((1+0.5*(1-cos(2*pi*x/L))).^-0.5).*((0<= x) & (x <=L))+(1).*((x >L)));
%%% Initial Conditions %%%
u(1)=01;
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')
Farooq Hussain
2020년 4월 12일
my code does not read the value of A(x)...this is the issue
Ameer Hamza
2020년 4월 12일
Variables a and L are not defined in your code. Also can you show me the mathematical equation you are trying to implement.
Farooq Hussain
2020년 4월 12일
can i take your email please...so that i can email you the pdf of the paper i have reviewed...i manged to do all coding and plot graphs.....every thing is clear to me but piece-wise function A(x) is not clear to me...have been trying by different methods.....but there is something i am missing....Thanking in anticpation
Farooq Hussain
2020년 4월 12일
here is the link of that paper:
Ameer Hamza
2020년 4월 12일
It is not practical for me to understand the complete paper. Can you write down the logic to generate the matrix A, and I will try to suggest how it can be written in MATLAB.
Farooq Hussain
2020년 4월 12일
편집: Farooq Hussain
2020년 4월 12일
simply how can i incorporate or use A(x) by using this RK code...
A(x) in MATLAB code...I had successfully used A(x) in Mathematica.... I will send you my complete code after simplifying..so that you have no problem in understanding this...
Finally, thanks for your cooperation and time..I really appreciate this.
Ameer Hamza
2020년 4월 12일
I haven't used Mathematica in a while, so cannot remember correctly. In MATLAB, you don't need to explicitly write an index on the left-hand side if you are initializing a variable. You can just write
A = (1).*((x < 0)) + ((1-0.5*CM*(1-cos(2*pi*x/L))).^-0.5).*((0<= x) & (x <=L))+(1).*((x >L));
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
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
2020년 4월 12일
편집: Thiago Henrique Gomes Lobato
2020년 4월 12일
0 개 추천
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)
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
