# i cant understand why matlab assumpt that my variables have image part

2 Ansichten (letzte 30 Tage)
shahin hashemi am 27 Nov. 2017
Kommentiert: Walter Roberson am 27 Nov. 2017
dear all
i run the following code :
syms be(i) ga(i) eb(i)
for i=2:8
k(i)=sqrt(((be(i))^2)+((ga(i))^2));
q(i)=atan2((ga(i)),(be(i));
teta(i)=k(i)*L;
pl(:,:,i)=[cos(q(i))*(1-cos(teta(i)))/k(i); sin(q(i))*(1-cos(teta(i)))/k(i); sin(teta(i))/k(i)];
end
and answer is pl(:,:,2)......pl(:,:,8) but in every pl like pl(:,:,2) as i showed below 1i apear that i cant understad why it showed up and also why abs and real and image apear while my variable as a angle are real and these equation cant have image part
pl(:,:,2) =
((imag(ga(2)) - real(be(2)))*(cos((3*(be(2)^2 + ga(2)^2)^(1/2))/100) - 1))/(abs(be(2) + ga(2)*1i)*(be(2)^2 + ga(2)^2)^(1/2))
-((cos((3*(be(2)^2 + ga(2)^2)^(1/2))/100) - 1)*(imag(be(2)) + real(ga(2))))/(abs(be(2) + ga(2)*1i)*(be(2)^2 + ga(2)^2)^(1/2))
sin((3*(be(2)^2 + ga(2)^2)^(1/2))/100)/(be(2)^2 + ga(2)^2)^(1/2)
i really apreciated if you could help me that what should i do to assumption all of my variable to be real and get ride of additonal i, abs and real that apear
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Walter Roberson am 27 Nov. 2017
In MATLAB, syms be(i) declares that be is a symbolic function with dummy variable i . It does not declare that be is a vector. Inside the for loop, be(i) would declare that that (unknown) function is to be called passing in the integer i as an argument.
In MATLAB, it is not possible to put assumptions on the output of a function.
syms L real
be = sym('be', [1 8], 'real');
ga = sym('ga', [1 8], 'real');
q = sym('q', [1 8], 'real');
for i=2:8
k(i) = sqrt(((be(i))^2)+((ga(i))^2));
q(i) = atan2(ga(i), be(i));
teta(i) = k(i)*L;
pl(:,:,i)=[cos(q(i))*(1-cos(teta(i)))/k(i); sin(q(i))*(1-cos(teta(i)))/k(i); sin(teta(i))/k(i)];
end
The final step to get rid of the abs is:
rewrite(pl, 'sqrt')
##### 3 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Walter Roberson am 27 Nov. 2017
"and again when i try to compute R in my complete cod again i and abs apear :("
You did not rewrite() R, you rewrite(Rl) .
"rewrite(expr,target) rewrites the symbolic expression expr in terms of the target function target. The rewritten expression is mathematically equivalent to the original expression."
In short, rewrite() is the routine that knows about mathematical equivalences such as abs(a+b*i) meaning sqrt(a^2+b^2) when a and b are real, and that exp() of imaginary numbers can be rewritten as sin() and cos() terms.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Assumptions in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!