How to give if...then condition ?

How can I give a condition statement for the following case:
for A1<=A,
e=0
for A1>A,
e=5e-03
Now, I want to use this e value (after A1>A) to calculate a new value of e at the next time instant. Say, the formula for e is,
e = v/[u/e - 2] (e on the RHS is the value of e at the previous time step and the e on LHS is the new value which I want)
How can I form a condition for the above case??
Thanks!!

댓글 수: 1

Matt Fig
Matt Fig 2012년 10월 18일
편집: Matt Fig 2012년 10월 18일
You need to learn the difference between FOR and IF:
doc for
doc if
Once you have this difference clear in your mind, you should be able to formulate your question more clearly.

댓글을 달려면 로그인하십시오.

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 17일

0 개 추천

e=5e-03
if A1<=A,
e=0
else
e = v/[u/e - 2]
end

댓글 수: 15

Urvi
Urvi 2012년 10월 18일
It isn't working for my code! Thanks a lot anyway!
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 18일
show your code
Urvi
Urvi 2012년 10월 19일
I can't thats the problem! it is so frustrating! is it okay if i provide a similar code?
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
편집: Azzi Abdelmalek 2012년 10월 19일
Maybe you should reformulate your question and make it clear.
Urvi
Urvi 2012년 10월 19일
function H = newmain
global b1 b2 b3 c
b1=0;
b2=2.0;
b3=0.33;
options=odeset('InitialStep', 0.01, 'MaxStep', 0.01, 'RelTol', 10., 'AbsTol',10.);
[t2,y2]=ode45(@equation,[0:0.05:0.1],[1 2 0], options);
H = [t2 y2]
end
function dy=equation(t,y)
global b1 b2 b3 c
dy=zeros(3,1);
b1=b1+1-exp(-b3)
b2=b1-y(1)
b3=b1+b2+y(2)
dy(1)=-b1*y(1);
dy(2)=b3*y(1)+b2*y(2);
dy(3)=sqrt(b1)+y(1)+y(3);
c=[c;t b1 b2 b3]
end
In the above I want to induce a condition say,
if y(1)<100 && b1<0.0001 %(initially b is zero)
b1=0.01
end
Now I want to use this new value of to calculate a new value b1 from the formula of b1. Again return a new value for the same...how can I do that? Thanks!
try this
function dy=equation(t,y)
global b1 b2 b3 c
dy=zeros(3,1);
b1=b1+1-exp(-b3)
b2=b1-y(1)
b3=b1+b2+y(2)
if y(1)<100 & b1<0.0001 %(initially b is zero)
b1=0.01
end
dy(1)=-b1*y(1);
dy(2)=b3*y(1)+b2*y(2);
dy(3)=sqrt(b1)+y(1)+y(3);
c=[c;t b1 b2 b3]
end
Urvi
Urvi 2012년 10월 19일
I used this same logic for my code but it isn't calculating a new value for "b1".
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
편집: Azzi Abdelmalek 2012년 10월 19일
Because the condition b1<0.0001 is always false
look at the result
find(c(:,2)<0.0001) % c(:,2) is b1,
the result is
b1 =
Empty matrix: 0-by-1
Urvi
Urvi 2012년 10월 19일
Initially b is zero so b1<0.0001. So should I try b1>=0 ?
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
편집: Azzi Abdelmalek 2012년 10월 19일
No, when the function is called, first you have b1=b1+1-exp(-b3), before the test. b1 is not zero
Urvi
Urvi 2012년 10월 19일
편집: Urvi 2012년 10월 19일
So, now how should I formulate my condition?
b1 is less than zero. I just calculated it. Maybe in the later time steps, it does not abide by the condition i gave.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
편집: Azzi Abdelmalek 2012년 10월 19일
function dy=equation(t,y)
global b1 b2 b3 c
dy=zeros(3,1);
if y(1)<100 & b1<0.0001
b1=0.01
else
b1=b1+1-exp(-b3)
end
b2=b1-y(1)
b3=b1+b2+y(2)
dy(1)=-b1*y(1);
dy(2)=b3*y(1)+b2*y(2);
dy(3)=sqrt(b1)+y(1)+y(3);
c=[c;t b1 b2 b3]
end
Urvi
Urvi 2012년 10월 19일
I shall try this as well. I have one more question related to the above code only.
Initially b1=0. so now i want to have a condition as follows:
if y(1)>b2 %(b2 is also changing with time)
b1=1e-03.
(Now use this new value of b1 in the following formula for y(1)>b2)
b1=b1+1-exp(-b3)
end
(There is no 'else' part in this condition)
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
편집: Azzi Abdelmalek 2012년 10월 19일
function dy=equation(t,y)
global b1 b2 b3 c y1 yy
dy=zeros(3,1);
if y(1)>b2
b1=0.001;
end
b1=b1+1-exp(-b3);
b2=b1-y(1);
b3=b1+b2+y(2);
dy(1)=-b1*y(1);
dy(2)=b3*y(1)+b2*y(2);
dy(3)=sqrt(b1)+y(1)+y(3);
c=[c;t b1 b2 b3] ;
Urvi
Urvi 2012년 10월 19일
Thank you.

댓글을 달려면 로그인하십시오.

Sachin Ganjare
Sachin Ganjare 2012년 10월 18일

0 개 추천

if A1<=A
e= 0;
elseif A1>A
e = 5e-3;
end
e= v/(u/e_prev - 2);
e_prev = e;
Hope it helps!!!

댓글 수: 6

Urvi
Urvi 2012년 10월 19일
Its not working :(
Sachin Ganjare
Sachin Ganjare 2012년 10월 19일
Can you describe the error?
Urvi
Urvi 2012년 10월 19일
there is no error msg as such....it just uses 5e-03 as the value of e. it isnt calculating a new value of e.
Sachin Ganjare
Sachin Ganjare 2012년 10월 19일
Can you show your code, if possible a pseudo code, to help understand the problem.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
Urvi, Can you post your code?
Urvi
Urvi 2012년 10월 19일
ya I just did..thank you.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2012년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by