complex number representation as real and imaginary part

31 views (last 30 days)
syms x real
syms t real
e=cos(x)+1i*sin(x)
real(e)
u=(cos(x)+1i*sin(x))/((1+((1/(0.1)^4)-1)*(cos(4*t)+1i*sin(4*t))))
v=sym(((1/(0.1)^4)-1))*(cos(4*x)+1i*sin(4*x))
m1=real(u)
m2=(m1)^(1/4)
x=0.5:0.5:5
t=1
% mu=0.1
row=0
for i=1:length(x)
row=row+1
col=0
for j=1:length(t)
col=col+1
u(row,col)=m2(x(i),t(j));
end
end
The above code shows error when I put the power (1/4) on the denominator of u=(cos(x)+1i*sin(x))/((1+((1/(0.1)^4)-1)*(cos(4*t)+1i*sin(4*t)))) . I dont get it why it is so ? So I find the real part separtely and find the real part . But now again the problem with the for loop why the error is there ? But is the option for that

Accepted Answer

Sameer
Sameer on 6 Dec 2024 at 6:24
It looks like there are a few issues in your code
1. When you try to take the real part and then raise it to the power of 1/4, it might not work as expected due to the complex nature of the expression.
2. The error in the for loop is likely due to the way you are trying to index and assign values to 'u'. Since 'u' is initially defined as a symbolic expression, you need to ensure that you're evaluating it correctly for each value of 'x' and 't'.
Here's how you can revise your code:
syms x t real
e = cos(x) + 1i*sin(x);
u = (cos(x) + 1i*sin(x)) / (1 + ((1/(0.1)^4) - 1) * (cos(4*t) + 1i*sin(4*t)));
v = sym(((1/(0.1)^4) - 1)) * (cos(4*x) + 1i*sin(4*x));
% Extract the real part of u
m1 = real(u);
% Raise the real part to the power of 1/4
m2 = m1^(1/4);
% Define the range for x and t
x_vals = 0.5:0.5:5;
t_vals = 1;
% Initialize the result matrix
result = zeros(length(x_vals), length(t_vals));
% Evaluate m2 for each combination of x and t
for i = 1:length(x_vals)
for j = 1:length(t_vals)
result(i, j) = double(subs(m2, {x, t}, {x_vals(i), t_vals(j)}));
end
end
disp(result);
0.0696 + 0.0696i 0.0705 + 0.0705i 0.0669 + 0.0669i 0.0568 + 0.0568i 0.0516 + 0.0000i 0.0857 + 0.0000i 0.0968 + 0.0000i 0.1000 + 0.0000i 0.0968 + 0.0000i 0.0857 + 0.0000i
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!