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

3 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
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 Kommentare
Walter Roberson
Walter Roberson am 27 Nov. 2017
"it make be as be1 be2 be3 ....... be8 but i need them in this form be(1) be(2) ..... so i can solve my equations in fsolve is there any solutions for my problem"
Use
p1func = matabFunction(pl, 'vars', {L, be, ga, , eb});
This would use a separate variable for L, would bundle all of be into one parameter, would bundle all of ga into one parameter, would bundle all of eb into one parameter.
Walter Roberson
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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by