Using If for syms

Hi,
I hope you are keeping safe.
I have a urgent question in matlab. Could you please give me sime advice in this regard. I wrote following code where
"function F = fun(x,y,alpha1,alpha2,beta1,beta2,lambda)
F= log((1+alpha1*x./(1+alpha2*y))) -lambda*log((1+beta1*x./(1+beta2*y)));
end"
Main Code
Z=fun(x,P-x,alpha1,alpha2,beta1,beta2,lambda);
z=solve(Z==0);
for i=1:length(z)
z(i,1)=piecewise(z(i,1)<=0,0)
y(i)=P-z(i,1)
S(i)=fun(z(i,1),y(i),alpha1,alpha2,beta1,beta2,lambda)
end
I want to z(i,1)=0 if z(i,1)<=0 or complex and if z(i,1)>0, claculate S(i) but there is a problem with piecewise() which cannot be solved for me.
Could you please help me to write it. P, alpha1, alpha2, beta1, beta2, and lambda are desired positive valuse.
Many Thanks in advance.

Risposte (1)

Walter Roberson
Walter Roberson il 6 Mar 2021

0 voti

subs(piecewise(x<3,0),x,5)
ans = NaN
That is, if no condition is matched by the piecewise then the output is undefined, NaN. Not 0 or empty.
piecewise(imag(z(i))~=0 || z(i)<0, 0, S(i))

6 Commenti

Hossein Alishahi
Hossein Alishahi il 6 Mar 2021
Thank you for your answer but there is a problem. Image is a double type argument for piecewise which is not accepted.
On the other hand, as I said I am looking for three target
1.S(i)=0 if z(i,1)<=0
2.S(i)=0 if z(i,1)= complex
3. S(i)=clculate if z(i,1)>0
Best,
Walter Roberson
Walter Roberson il 6 Mar 2021
I didn't use image(), I used imag()
piecewise(imag(z(i))~=0 || z(i)<=0, 0, S(i))
Hossein Alishahi
Hossein Alishahi il 6 Mar 2021
I made a typo but I meant imag(). I copied your code on matlab and it annouced me again a similar error .
Walter Roberson
Walter Roberson il 6 Mar 2021
I will need the values for the constants to test with
Hossein Alishahi
Hossein Alishahi il 6 Mar 2021
Thank you for your consideration.
I give you two case which meet all states
1.P=3, alpha1=1, alpha2=0.2, beta1=1, beta2=2, and lambda=2.
2.P=3, alpha1=1, alpha2=2, beta1=3, beta2=4, and lambda=5
Thanks in advance

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by