- #26

- 690

- 33

It works now. I'll implement the same code using a function as suggested soon. I had thought that Matlab is more of like a calculator than a language like C++ but it looks like I was wrong.

Matlab:

```
%file name fourier.m
clear all; close all; clc;
f = [0.001]; %defining and initializing a constant vector
%0 Hz to 100 Hz, with 0.1 step, there would be 1000 frequencies
cos_v=0.*[1:500000]'; %defining and initializing vector to store values
%a vector with 1000 zeroes
f_w=[0]; % defining and initializing a vector, fourier transform,
user_decision = 'y';
while user_decision == 'y'
x = input('Enter value of x: ');
for i1=[1:500000] % for loops begins, with loop index i1
f_w = sqrt(1/((pi^2)*f)); % fourier transform of 1/sqrt(x)
cos_v(i1)=f_w.*(cos(2*pi*f*x)); % i1 serves as an index for cos_v vector elements too
f=f+0.001; %incrementing f
f_w=0; %setting f_w=0 for next iteration
end
combined_sinusoids=sum(cos_v); % cos_v(1)+cos_v(2)+cos_v(3)+...
y = combined_sinusoids;
fprintf('The value of combined_sinusoids is %d \n',y)
fprintf('The value of 1/sqrt(x) is %d \n',(1/sqrt(x)))
fprintf('Difference between 1/sqrt(x) and approximation by combined_sinusoids is %d \n',abs(y-(1/sqrt(x))))
user_decision=input('Would you like to continue, enter Y/N: ','s');
if user_decision=='n'
break
end
end
```