How to do summation by using for loop in matlab [duplicate] - matlab

I have the following series
I tried this code but it does not print the final result...instead gives a long line of numbers!
syms n
y = symsum(1/sqrt(n),[1,100])
Result:
y =
2^(1/2)/2 + 3^(1/2)/3 + 5^(1/2)/5 + 6^(1/2)/6 + % and so on....
So the question is how to produce a final number as answer?!
Should I go with a script like this instead?
y = 0;
for i = 1:1:100
y = y + (1/sqrt(i));
end
disp(y);

To answer the original question, you can convert the symbolic expression you initially got using double, to convert from a symbolic to a numeric value:
y = double(y)
Or actually:
syms n
y = double(symsum(1/sqrt(n),[1,100]))
and you get 18.5896.
Additionally, you can use eval to evaluate the symbolic expression (thanks Luis Mendo).
Yay!

how about dropping the loop and use this instead:
n=1:100
result = sum(1./sqrt(n))
>> result =
18.5896
I'm not sure if you want to use the symbolic sum of series function in your case since you are only dealing with a simple function.

Related

Implementing my own FFT in MATLAB giving wrong results

I'm trying to implement my own fft in MATLAB the following way:
function z=FastFourierTransform(x)
N=length(x);
if N <= 1
z = x;
else
range = (0:N/2-1);
e = exp(-2i*pi/N).^range;
odd = FastFourierTransform(x(1:2:N-1));
even = e.*FastFourierTransform(x(2:2:N));
z = [even + odd, even - odd];
end
return
Turns out, there seems to be somthing wrong with it since it does not give the same result as the built in function provided by MATLAB.
I'm calling the function the following way:
N = 128;
x = 32*pi*(1:N)'/N;
u = cos(x/16).*(1+sin(x/16));
actualSolution = fft(u);
actualSolution
mySolution = FastFourierTransform(u)';
mySolution
actualSolution
mySolution
The numbers are always the same but they sometimes differ in their sign.
You have swapped odd and even.
Using this line to compute z will produce the correct FFT:
z = [odd + even, odd - even];
My guess is that the source of confusion is that Matlab uses 1-based indices, and the pseudocode you used to implement the function uses 0-based indices.

Two functions in Matlab to approximate integral - not enough input arguments?

I want to write a function that approximates integrals with the trapezoidal rule.
I first defined a function in one file:
function[y] = integrand(x)
y = x*exp(-x^2); %This will be integrand I want to approximate
end
Then I wrote my function that approximates definite integrals with lower bound a and upper bound b (also in another file):
function [result] = trapez(integrand,a,b,k)
sum = 0;
h = (b-a)/k; %split up the interval in equidistant spaces
for j = 1:k
x_j = a + j*h; %this are the points in the interval
sum = sum + ((x_j - x_(j-1))/2) * (integrand(x_(j-1)) + integrand(x_j));
end
result = sum
end
But when I want to call this function from the command window, using result = trapez(integrand,0,1,10) for example, I always get an error 'not enough input arguments'. I don't know what I'm doing wrong?
There are numerous issues with your code:
x_(j-1) is not defined, and is not really a valid Matlab syntax (assuming you want that to be a variable).
By calling trapez(integrand,0,1,10) you're actually calling integrand function with no input arguments. If you want to pass a handle, use #integrand instead. But in this case there's no need to pass it at all.
You should avoid variable names that coincide with Matlab functions, such as sum. This can easily lead to issues which are difficult to debug, if you also try to use sum as a function.
Here's a working version (note also a better code style):
function res = trapez(a, b, k)
res = 0;
h = (b-a)/k; % split up the interval in equidistant spaces
for j = 1:k
x_j1 = a + (j-1)*h;
x_j = a + j*h; % this are the points in the interval
res = res+ ((x_j - x_j1)/2) * (integrand(x_j1) + integrand(x_j));
end
end
function y = integrand(x)
y = x*exp(-x^2); % This will be integrand I want to approximate
end
And the way to call it is: result = trapez(0, 1, 10);
Your integrandfunction requires an input argument x, which you are not supplying in your command line function call

Factor symbolic expression involving exp()

I have a symbolic function exp(a+b), and would like to factor out A=exp(a) to produce exp(a+b) = A*exp(b), but I cannot figure out how to do this in MATLAB. Below is my attempt:
syms a b A
X = exp(a+b);
Y = subs(X,exp(a),A) % = A*exp(b)
however, Y = exp(a+b). For some reason, MATLAB cannot determine:
exp(a+b) = exp(a) * exp(b) = A*exp(b).
Any help is greatly appreciated.
First, expand the expression so that the exponents are separated then do the substitution. By default, when writing out an expression for the first time (before running it through any functions), MATLAB will try and simplify your expression and so exp(a)*exp(b) can be much better expressed using exp(a+b). This is why your substitution had no effect. However, if you explicitly want to replace a part of the expression that is encompassed by an exponent with a base, expand the function first, then do your substitution:
>> syms a b A;
>> X = exp(a+b);
>> Xexpand = expand(X)
Xexpand =
exp(a)*exp(b)
>> Y = subs(Xexpand, exp(a), A)
Y =
A*exp(b)

Sum of series in matlab using symsum

I have the following series
I tried this code but it does not print the final result...instead gives a long line of numbers!
syms n
y = symsum(1/sqrt(n),[1,100])
Result:
y =
2^(1/2)/2 + 3^(1/2)/3 + 5^(1/2)/5 + 6^(1/2)/6 + % and so on....
So the question is how to produce a final number as answer?!
Should I go with a script like this instead?
y = 0;
for i = 1:1:100
y = y + (1/sqrt(i));
end
disp(y);
To answer the original question, you can convert the symbolic expression you initially got using double, to convert from a symbolic to a numeric value:
y = double(y)
Or actually:
syms n
y = double(symsum(1/sqrt(n),[1,100]))
and you get 18.5896.
Additionally, you can use eval to evaluate the symbolic expression (thanks Luis Mendo).
Yay!
how about dropping the loop and use this instead:
n=1:100
result = sum(1./sqrt(n))
>> result =
18.5896
I'm not sure if you want to use the symbolic sum of series function in your case since you are only dealing with a simple function.

how do I compute this infinite sum in matlab?

I want to compute the following infinite sum in Matlab, for a given x and tau:
I tried the following code, given x=0.5 and tau=1:
symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,1,inf)
But I get this:
(228155022448185*sum((exp(-pi^2*n^2)*((exp(-(pi*n*i)/2)*i)/2 - (exp((pi*n*i)/2)*i)/2)^2)/n^2, n == 1..Inf))/281474976710656
I want an explicit value, assuming the sum converges. What am I doing wrong? It seems like Matlab doesn't compute exp() when returning symsum results. How do I tell Matlab to compute evaluate the exponentials?
Convert to double
double(symsum(...))
Just to show you a different way, one that does not require the symbolic toolbox,
summ = 0;
summP = inf;
n = 1;
while abs(summP-summ) > 1e-16
summP = summ;
summ = summ + sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n;
n = n + 1;
end
8/pi/pi * summ
which converges after just 1 iteration (pretty obvious, since exp(-4*6.28..)/n/n is so tiny, and sin(..) is always somewhere in [-1 1]). So given tau==1 and x==0.5, the infinite sum is essentially the value for n==1.
You should first define your variable "n" using syms. Then, you can include this variable in your symsum code.
Here's what I did:
syms n; AA = symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,n,1,inf); BB = double(AA)
BB = 4.1925e-05