Plot sinusoid equation of x(t)
2 次查看(过去 30 天)
显示 更早的评论
I am trying to solve this problem using a MATLAB. I have attached the problem itself along with the code I have written so far. When I run the code I get an error that says 'Array indices must be positive integers or logical values.'
t = 0:5;
x = sqrt(2)*(1+1i)*exp(1i*(pi/4))*exp((-1+((1i)*2*pi)*t));
re = real(x);
imaginary = imag(x);
plot(re)
hold on
plot(imaginary)
0 个评论
采纳的回答
Image Analyst
2023-1-19
This works just fine:
t = linspace(0, 5, 500);
x = sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+((1i)*2*pi)*t));
re = real(x);
imaginary = imag(x);
plot(re, 'b-', 'LineWidth', 2)
hold on
plot(imaginary, 'r-', 'LineWidth', 2)
legend('Real Part', 'Imaginary Part', 'Location', 'northwest')
grid on;
xlabel('t');
ylabel('x real or imaginary part')
title('x = sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+((1i)*2*pi)*t))')
8 个评论
Torsten
2023-1-20
x = @(t)sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+(1i)*2*pi)*t);
t = linspace(-2, 2, 400);
hold on
plot(t,real(x(t)))
plot(t,imag(x(t)))
plot(t,x(t+2)+conj(x(t+2)))
hold off
grid on
更多回答(2 个)
John D'Errico
2023-1-19
编辑:John D'Errico
2023-1-19
Do not define a variable with the names: sqrt, exp, real, imag, plot
If you do, then what do you think will happen when you then try to use the functions of the same name? Should MATLAB know what you want it to do? Sorry, but computers are very literal things. They try to do what you tell them to do. But when you tell them to do something confusing, what do you expect?
Do this at the command line:
which sqrt -all
which exp -all
which real -all
which imag -all
which plot -all
One of those will tell you that you have created a variable by that name. Delete the offending variable. My guess would be the problem lies in one of the names exp, real, or imag.
You could also have just looked at the list of variable names. So my guess is this would tell you something useful:
whos exp real imag
As if any of those variable names were used by you, they would now show up.
3 个评论
Torsten
2023-1-19
Do you know what exp(2*pi*1i*t) is for t=0:5 ? So the plot result shouldn't surprise you.
Sydney Lang
2023-1-19
You haven't defined t. If you do, the code works.
Additionally you haven't mentioned where this error occurs. I assume its coming from how you defined t?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!