Converting equation into Octave / Matlab code and a for loop

657 Views Asked by At

I have an array of thousands of values I've only included three groupings as an example below:

(amp1=0.2; freq1=3; phase1=1; is an example of one grouping)

t=0;
amp1=0.2; freq1=3; phase1=1;   %1st grouping
amp2=1.4; freq2=2; phase2=1.7; %2nd grouping
amp3=0.8; freq3=5; phase3=1.5; %3rd grouping

The Octave / Matlab code below solves for Y so I can plug it back into the equation to check values along with calculating values not located in the array.

clear all
t=0;
Y=0;
a1=[.2,3,1;1.4,2,1.7;.8,5,1.5]
for kk=1:1:length(a1)
    Y=Y+a1(kk,1)*cos ((a1(kk,2))*t+a1(kk,3))
    kk
end
Y

These are the formulas I've tested but I'm not sure how to put it into a for loop that will work in an array of n groupings:

phase1_test=acos(Y/amp1-amp3*cos(2*freq3*pi*t+phase3)/amp1-amp2*cos(2*freq2*pi*t+phase2)/amp1)-2*freq1*pi*t
phase2_test=acos(Y/amp2-amp3*cos(2*freq3*pi*t+phase3)/amp2-amp1*cos(2*freq1*pi*t+phase1)/amp2)-2*freq2*pi*t
phase3_test=acos(Y/amp3-amp2*cos(2*freq2*pi*t+phase2)/amp3-amp1*cos(2*freq1*pi*t+phase1)/amp3)-2*freq2*pi*t

$$\left[ {\it phase_1}=\arccos \left({{y}\over{{\it amp_1}}}-{{ {\it amp_3}\,\cos \left(2\,{\it freq_3}\,\pi\,t+{\it phase_3}\right) }\over{{\it amp_1}}}-{{{\it amp_2}\,\cos \left(2\,{\it freq_2}\,\pi \,t+{\it phase_2}\right)}\over{{\it amp_1}}}\right)-2\,{\it freq_1} \,\pi\,t \right] $$

$$\left[ {\it phase_2}=\arccos \left({{y}\over{{\it amp_2}}}-{{ {\it amp_3}\,\cos \left(2\,{\it freq_3}\,\pi\,t+{\it phase_3}\right) }\over{{\it amp_2}}}-{{{\it amp_1}\,\cos \left(2\,{\it freq_1}\,\pi \,t+{\it phase_1}\right)}\over{{\it amp_2}}}\right)-2\,{\it freq_2} \,\pi\,t \right] $$

$$\left[ {\it phase_3}=\arccos \left({{y}\over{{\it amp_3}}}-{{ {\it amp_2}\,\cos \left(2\,{\it freq_2}\,\pi\,t+{\it phase_2}\right) }\over{{\it amp_3}}}-{{{\it amp_1}\,\cos \left(2\,{\it freq_1}\,\pi \,t+{\it phase_1}\right)}\over{{\it amp_3}}}\right)-2\,{\it freq_3} \,\pi\,t \right] $$

Image of formula below: image of formula

I would like to do a check / calculate phases if given a freq(x) and amp(x) values.

I know I have to do a for loop but how do I convert this equation into a for loop so it will work on n groupings in an array and calculate different values not found in the array?

Basically I would be given an array of n groupings and freq=2.5 and amp=.23 and use the formula to calculate phase. Note: freq will not always be in the array hence why I'm trying to calculate the phase(x) using a formula.

How would I write the equation / for loop for finding the phase if I want to find freq=2.5 and amp=.23 and the phase is unknown

One of the suggestions was it may require writing non linear equations which I'm not sure how to convert what I'm trying to do into such an equation any ideas?

1

There are 1 best solutions below

2
On BEST ANSWER

Here is some pseudo code (I would use struct arrays as they are nicer and easy to see things but thats for you to learn later)

% this is to compute the argument easier you could refactor this part to be more vectorised.
for kk=1:length(a1)   
   cosine_terms(kk) = amp(kk)*cos(2.0 pi freq(kk) + phase(kk);
end
sum_of_cosines = sum(cosine_terms);

for kk=1:length(a1)
    temp_sum = sum_of_cosines - amp(kk)*cos(2.0*pi*freq(kk)+phase(kk);
    phase_test(kk) = arcos((Y-temp_sum)/amp(kk) - 2.0*pi*freq(kk)*t);
end

This is code that will probably not run. But I think you get the jist?