Your script has several errors, but they are all easily resolved.
I will initially put some tips on how to solve problems when programming!
-
Pay attention to Matlab's return
When you run something and crash, interpreter returns an error.
In your case:
Subscript indices must either be real positive integers or logicals.
Unlike python starting at 0,
From now on, I assume all bugs related to this have been fixed!
- Check how a function works before using
You can use help nome_da_funcão
on the command line to see what it needs to input and what it 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 line). This is what you are using as coefficients.
will have the result
plot(column,real(imag_number_column))
Smaller things that are good to know:
clear all
In general, use only clear
, as Matlab saves part of the code in memory to optimize the time to interpret & roll.
figure
, plot
and hold
.
When using multiple graphics in the same figure, use this in sequences:
figure
plot
hold
plot
or
figure
hold
plot
plot
or even without declaring figure
. Matlab will create a figure if there is none when hold or plot is called. And the execution of the chart is immediate (unlike, for example, matplotlib that needs draw
to draw the graph). When a plot is not in hold
, it will delete 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 only one point. I do not think it's the result you want, but that's what you stated in
x_aprox_0=a(1)+a_neg(1) ;
where the two values are constant (see that I have 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);
See 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 have not made clear what the code does, and you can edit your question and comment on my answer to understand us better.
So I understand you have the signal at different frequencies to make a Fourier transform. In that you are failing, but as I am not sure if this is what you want, I will write my ideas. If so, I'll post the code later, editing my answer:
You want 4 frequencies plus the original sine.
Each signal will be added to the previous signal.
The plot will contain the 5 signals, the original plus the sums.
If this is the case, there are a few ways to write the answer. Yours is not far from that, 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.