My fft2 function not work with me - matlab

I faced a problem when I convert these two statements from build in to my own function:
gaus=gauss/sum(gauss(:));
BeforeAbs=fft2(gaus,size(im,1),size(im,2));
And it gave me [Attempted to access X(2); index out of bounds because numel(X)=1]
%--------Start convert from build in to my own function of Fourier transformation of 1 D
gaus=gauss/sum(gauss(:));
for u=1:(gaus)
summ=0;
for x=1:1
w2=(-1*(sqrt(-1)))*2*pi*((u*x)/(gaus+1))
summ=summ+(gaus(x)*exp(w2));
end
PQ2(u)=summ;
end%
X=size(im,1);
for u=1:(X)
summ=0;
for x=1:1
w3=(-1*(sqrt(-1)))*2*pi*((u*x)/(X+1))
summ=summ+(X(x)*exp(w3));
end
PQ3(u)=summ;
end
BeforeAbs=[PQ2 PQ3];
Can anyone tell me why this error appears with me?

In your code, the statement
X=size(im,1);
returns the size of the variable im along its first dimension. That would return a scalar value. However, you have a for loop:
for x=1:2
...
summ=summ+(X(x)*exp(w3));
end
that iterates over the values x = 1 and x = 2. When you try to evaluate X(x) when x = 2, you get the error because X has only one element.
Basically, you are doing something equivalent to this:
X = 5
X(2)
Also, your for-loop for u:
for u=0:(X-1)
starts from u = 0, but later you try to set
PQ3(u)=summ;
However, array indexing in MATLAB is 1-based, so PQ3(0) would result in an error. You should start indexing from 1: PQ3(1) = summ.

Related

Implementing forward Euler method in matlab

I'm trying to implement the forward Euler method using matlab, but don't understand the error I'm getting. This is what I have written:
function y = ForwardEulerMethod(f,y0,T,N)
h=T/N;
t=zeros(N+1,1);
for i=0:N
t(i)=i.*h; %line 5
end
y=zeros(N+1,1);
y(0)=y0;
for i=1:N
y(i)=y(i-1)+h.*f(t(i-1),y(i-1));
end
end
My error is with line 5 and says, "Subscript indices must either be real positive integers or logicals." I am familiar with this rule, but don't see how I'm breaking it. I'm just trying to replace a zero at each location in t with a numerical value. What am I missing?
Agree with #vijoc above. You are indexing with 0 at multiple places. You could either change how you are indexing the values or get rid of the for loop altogether, like below:
function y = ForwardEulerMethod(f,y0,T,N)
h=T/N;
t=0:N .* h; % this takes the place of the first for-loop
y=zeros(N+1,1);
y(1)=y0;
for i=2:N+1
y(i)=y(i-1)+h.*f(t(i-1),y(i-1));
end
end
You could even replace the second loop if the function f takes vector inputs like so:
y(1) = y0;
y(2:end) = y(1:end-1) + h .* f(t(1:end-1), y(1:end-1));
You're iterating over i = 0:N and using it as t(i)=i.*h, so you're trying to access t(0) during the first iteration. Matlab indexing starts from 1, hence the error.
You also have other lines which will cause the same error, once the execution gets that far.

How to resolve MATLAB trapz function error?

I am working on an assignment that requires me to use the trapz function in MATLAB in order to evaluate an integral. I believe I have written the code correctly, but the program returns answers that are wildly incorrect. I am attempting to find the integral of e^(-x^2) from 0 to 1.
x = linspace(0,1,2000);
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = e.^(-(x(iCnt)^2));
end
a = trapz(y);
disp(a);
This code currently returns
1.4929e+03
What am I doing incorrectly?
You need to just specify also the x values:
x = linspace(0,1,2000);
y = exp(-x.^2);
a = trapz(x,y)
a =
0.7468
More details:
First of all, in MATLAB you can use vectors to avoid for-loops for performing operation on arrays (vectors). So the whole four lines of code
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = exp(-(x(iCnt)^2));
end
will be translated to one line:
y = exp(-x.^2)
You defined x = linspace(0,1,2000) it means that you need to calculate the integral of the given function in range [0 1]. So there is a mistake in the way you calculate y which returns it to be in range [1 2000] and that is why you got the big number as the result.
In addition, in MATLAB you should use exp there is not function as e in MATLAB.
Also, if you plot the function in the range, you will see that the result makes sense because the whole page has an area of 1x1.

Using MATLAB to compute all the integers the satisfy a condition

I want to compute using MATLAB all the integers x such that x^2-2x<2000, but I am having problems in the displaying part because when I run my code MATLAB seems not to finish. My code is as follows:
x=100;
while x^2+2x-2000>=10^-6
x=x-20;
if x^2+2x-2000<10^-6
disp(x)
end
end
I think the wrong part is when I type disp(x) in the while loop, but I don't know how to fix it.
NOTE: I am using the 10^-6 to avoid rounding errors
Can someone help me to fix this code please?
Computationally, finding all integers that satisfy this condition will need some help from a quick insight into this problem. Otherwise, you'll have to test all the integers, which is impossible since there is infinite number of integers. Analytically, finding all the integers that satisfy the condition x^2-2x < 2000 means finding the integers that lies within the intersection of the curve x^2 - 2x and y = 2000.
Let's first take a look at the problem by plotting it:
x = -500:.1:500;
y = x.^2 - 2*x;
plot(x, y, 'color', 'g')
hold on
line([-200 200], [2000 2000], 'color', 'r')
You can easily see that you can limit your search to at least between -100 and 100. You can store the value in an array
results = [] % Declare empty array first and append value later.
for x = -100:100
if x^2 - 2*x < 2000
disp(x)
results = [results,x]; % Using a bracket to append value to the array.
end
end
And a faster way to get results using logical indexing.
x = -100:100
results = x(x.^2 - 2*x<2000)
In the above code, x.^2 - 2*x < 2000 generates a logical array that has the same size as x. Each element stores the logical value that comes from the evaluating each element in x with the expression. You then use this as a mask to pick out only elements in x that satisfy this condition.
If you add in some parentheses and use the correct syntax for 2*x, it works.
x=100;
while (x^2+2*x-2000)>= (10^-6)
x=(x-20);
if (x^2+2*x-2000) <10^-6
disp(x)
end
end
Building upon #User3667217's solution, you can also vectorise this:
x = -100:100;
y = x.^2-2*x;
tmp = y<2000;
results = y(tmp);
this will give you a speed up over the for-loop solution.

Please Help: In an assignment A(I) = B, the number of elements in B and I must be the same. MATLAB

I was asked in an assignment to use Euler's method to determine the values of t and y from t=0:1000. I have all the basic code and parameters down but when i put my Euler's equation in I get the error code
In an assignment A(I) = B, the number of
elements in B and I must be the same.
Error in Project1 (line 24)
Ay(i+1) = Ay(i) + (dAy)*x;
How could I change these variables between vectors and scalars to allow the equation to run? My full code can be found below:
dt=x;
Ay=zeros(1,1001);
Ay0=1250;
Ay(1) = Ay0;
t=0;
y=0;
t=0:dt:1000;
for i=1:1000
if y > 10
Qout=3*(y-10).^1.5;
else
Qout=0;
end
Qin=1350*sin(t).^2;
dAy=Qin-Qout;
Ay(i+1) = Ay(i) + dAy*dt;
end
plot(t,y);
The issue is that your variable "Qin" is not a number it is a vector containing sin values of the whole vector t. Similarly your "dAy" is also a vector. Hence it cannot be stored in a variable Ay.
if your dt =x = 1, just replace sin(t) with sin(i) i.e.
replace
Qin=1350*sin(t).^2;
by
Qin=1350*sin(i).^2;
The problem lies in the line of your code:
Ay(i+1) = Ay(i) + dAy*dt;
dAy*dt returns a vector.
When you add it to Ay(i) you still end up with a vector.
Ay(i+1) is a SINGLE element within a vector.
You Cannot assign a vector quantity to an element within a vector.

integral2 with fun calculated using vector

Im new to MATLAB. Want to use integral2 as follows
function num = numer(x)
fun=#(p,w) prod((p+1-p).*(1-w).*exp(w.*x.*x/2))
num= integral2(fun ,0,1,0,1)
end
I get several errors starting with
Error using .*
Matrix dimensions must agree.
Error in numer>#(p,w)prod(p+(1-w).*exp(w.*x.*x/2)) (line 5)
fun=#(p,w) prod(p+(1-w).*exp(w.*x.*x/2))
Can you please tell me what I do wrong.
Thanks
From the help for integral2:
All input functions must accept arrays as input and operate
elementwise. The function Z = FUN(X,Y) must accept arrays X and Y of
the same size and return an array of corresponding values.
When x was non-scalar, your function fun did not do this. By wrapping everything in prod, the function always returned a scalar. Assuming that your prod is in the right place to begin with and taking advantage of the properties of the exponential, I believe this version will do what you need for vector x:
x = [0 1];
lx = length(x);
fun = #(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(w).^sum(x.*x/2);
num = integral2(fun,0,1,0,1)
Alternatively, fun = #(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(sum(x.*x/2)).^w; could be used.