Your script has several errors, but they are all easily solved.
I will initially put some tips on how to solve problems when programming!
- Pay attention to the return of Matlab
When you spin something and fail, the interpreter returns an error.
In your case:
Subscript indices must either be real Positive integers or logicals.
Other than python starting at 0, Matlab starts counting in 1!
From now on, I assume all the errors related to this have been corrected!
- Check how a function works before using
You can use help nome_da_funcão
on the command line to see what she needs input and what she returns as output.
You use
a=zeros(N); %isso é uma matrix 5x5
However, you want to use
a=zeros(N,1); % ou a=zeros(1,N);
That gives you a column (or row). This is what you are using as coefficients.
will have the result
plot(column,real(imag_number_column))
Little things it’s good to know:
clear all
In general, use only clear
, because Matlab keeps part of the code in memory to optimize the time to interpret & run.
figure
, plot
and hold
.
When using several graphs in the same figure, use this in the sequences:
figure
plot
hold
plot
or
figure
hold
plot
plot
or even without declaring figure
. Matlab will create a picture if there is none when hold or Plot is called. And the chart execution is immediate (different from, for example, matplotlib who needs draw
to draw the graph). When a Plot is not in hold
, it will erase what is already plotted.
plot(t,x_aprox_0);
Look what you want to plot is the same thing you stated. In this case, x_aprox_0
is just a point. I don’t think it’s the result you want, but it’s what you stated in
x_aprox_0=a(1)+a_neg(1) ;
where the two values are constant (see that already corrected the index).
x_aprox_1=a(1)+a_neg(1)+a(2)*exp(1j*w0*t)+a_neg(2)*exp(-1j*w0*t);
Note that the values make sense. In this case, a_neg(2)
and a(2)
are close to zero.
Let’s go to the rest of your code. You haven’t made it clear what the code does, and you can edit your question and comment on my answer to understand us better.
I understand you have to have the signal on different frequencies to make a Fourier transform. You’re failing at this, but since I’m not sure that’s what you want, I’m going to write my letters. If that’s the case, I’ll put the code in later, editing my answer:
You want 4 frequencies plus the original sine.
Each signal will be added to the previous signal.
Plot will contain the 5 signals, the original plus the sums.
If this is the case, there are some ways to write the answer. Yours is not far from it, but the frequencies are not right. But if you want to modulate the intensity, your result is closer. But you have values very close to zero.