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)

 Réponse acceptée

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 commentaires

collegestudent
collegestudent le 20 Jan 2023
The graph for Real should look like this, is there a way to plot it like this in MATLAB?
Torsten
Torsten le 20 Jan 2023
Modifié(e) : Torsten le 20 Jan 2023
Change
t = linspace(0, 5, 500);
x = sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+((1i)*2*pi)*t));
to
t = linspace(-2, 2, 400);
x = sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+(1i)*2*pi)*t);
This is my updated code, however for x(t+2)+x(t+2) the plot is not produced and I get an error saying 'Array indices must be positive integers or logical values'
t = linspace(-2, 2, 400);
x = sqrt(2) * (1+1i) * exp(1i*(pi/4)) * exp((-1+(1i)*2*pi)*t);
re = real(x);
imaginary = imag(x);
eq = x.*(t+2)+x.*(t+2);
plot(re, 'b-', 'LineWidth', 2)
hold on
plot(imaginary, 'r-', 'LineWidth', 2)
hold on
plot(eq, 'LineWidth', 2)
legend('Real Part', 'Imaginary Part', 'x(t+2)+x*(t+2)', '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))')
Torsten
Torsten le 20 Jan 2023
Modifié(e) : Torsten le 20 Jan 2023
x(t+2)+x*(t+2)
does not mean
x*(t+2)+x*(t+2)
x is a function that depends on t.
You are asked to evaluate x and x* at t+2 and add the results.
collegestudent
collegestudent le 20 Jan 2023
But x and t are already defined so wouldnt it take the value of t and add 2 and then evaluates the x(t+2)
Torsten
Torsten le 20 Jan 2023
Modifié(e) : Torsten le 20 Jan 2023
For this part of the assignment, you should define x as a function handle:
x= @(t) t.^2;
t = 4;
x(t+2)
ans = 36
x(6)
ans = 36
collegestudent
collegestudent le 20 Jan 2023
Modifié(e) : collegestudent le 20 Jan 2023
But I already have x defined so I should rename it to be say xt when defining it as a function handle?
Update I added your suggestion to define x but it did not plot x(t+2)+x*(t+2)
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

Connectez-vous pour commenter.

Plus de réponses (2)

John D'Errico
John D'Errico le 19 Jan 2023
Modifié(e) : John D'Errico le 19 Jan 2023

0 votes

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 commentaires

I added the lines below at the beginning of my code and now the error has gone away, however the plot does not look like what I am trying to get
clear variables;
close all;
Torsten
Torsten le 19 Jan 2023
Do you know what exp(2*pi*1i*t) is for t=0:5 ? So the plot result shouldn't surprise you.
collegestudent
collegestudent le 19 Jan 2023
the time vector of t = 0:5; is not given in the equation. I just set it to be that for testing purposes, howeer the plot should be a sin wave with envolopes

Connectez-vous pour commenter.

Sydney Lang
Sydney Lang le 19 Jan 2023

0 votes

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?

1 commentaire

I adjusted my code but the error still occurs along with an error at line 5
t = 0:5;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by