# Help needed to fix a matlab code

• MATLAB
Thank you, @wle, @FactChecker .

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=; % 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

• FactChecker
wle
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.
Well to be fair Matlab's programming language isn't exactly stellar and the environment and community tend to treat programming as an afterthought.

Even using the approach you use, there are redundancies in your code:
Matlab:
     f = [0.001]; %defining and initializing a constant vector
Matlab:
    for i1=[1:500000] % for loops begins, with loop index i1
The square brackets are redundant in these cases. They're also misleading for f, since you're using it as a scalar. (Technically Matlab doesn't distinguish between a scalar and a 1x1 array, but it's still a good idea for the notation to reflect the use.) The variable i1 is also unnecessary if you vectorise your code. Also, you're writing a lot of comments that just repeat what the code already says, which isn't useful, and they seem to be getting out of sync with your code.

Matlab:
     cos_v=0.*[1:500000]'; %defining and initializing vector to store values
cos_v = zeros(1, 500000) is the more direct way to do this. (You should also consider making the size a variable or calling length(cos_v) instead of repeating the same array sizes/number of loop iterations multiple times.)

Matlab:
     f_w=; % defining and initializing a vector, fourier transform,
Matlab:
     f_w=0; %setting f_w=0 for next iteration
These assignments don't accomplish anything, since you bind f_w to the value you want to use at the beginning of the for loop.

Matlab:
    combined_sinusoids=sum(cos_v); % cos_v(1)+cos_v(2)+cos_v(3)+...
y = combined_sinusoids;
Why not just do y = sum(cos_v), or omit y and just use combined_sinusoids later in your code if you want to use a self-documenting name. (For that matter, if you want to use a for loop anyway why not compute the sum in the loop instead of constructing the vector cos_v?)

Matlab:
    if user_decision=='n'
break
end
You don't need this. You break out of the while loop anyway if user_decision is different from 'y'.

Last edited:
• PainterGuy
Hi,

Matlab:
>> x=2;
>> 1./{(5.*exp(3.*x))+(2.*exp(-3.*x))}
Undefined operator './' for input arguments of type 'cell'.

Hi,

Matlab:
>> x=2;
>> 1./{(5.*exp(3.*x))+(2.*exp(-3.*x))}
Undefined operator './' for input arguments of type 'cell'.
I removed the braces and used parentheses instead and it works. I'd say that as MATLAB is a programming language too so the use of braces trick MATLAB into thinking that the expression is an array or something of that sort. Thanks.

Matlab:
>> quad('1./((5.*exp(3.*x))+(2.*exp(-3.*x)))',0,3)

ans =

0.0594

I removed the braces and used parentheses instead and it works. I'd say that as MATLAB is a programming language too so the use of braces trick MATLAB into thinking that the expression is an array or something of that sort. Thanks.

Matlab:
>> quad('1./((5.*exp(3.*x))+(2.*exp(-3.*x)))',0,3)

ans =

0.0594
If you put braces around an expression, it turns it into a special kind of variable called a cell array. A cell array is not a numerical variable - it's a container for variables. You can't divide a number (1) by a container. See the FAQ to gain a better, more intuitive understanding of what cell arrays are. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

• PainterGuy