So I have the following two functions:
delta_t=T/N_time;
delta_x=1/N_space;
rho=delta_t/delta_x^2;
phi=zeros(N_space+1,N_time+1);
phi(:,1)=initial_condition((0:N_space)*delta_x);
for j=1:N_time;
for i=2:N_space;
phi(i,j+1)=rho*(phi(i-1,j)+phi(i+1,j))+(1-2*rho)*phi(i,j);
end;
end;
phi = phi(end,:);
end
When I run my second function, I get this error:
Error using ==> plot Vectors must be the same lengths.
I don't know why the sizes are different. They should be the same since the second function calls the first function. Any help would be much appreciated.
I think you have your phi transposed somehow. Your x_var is an 1 x (N_space + 1) vector, your phi is (N_space + 1) x (N_time + 1) and you return phi(end, :), which will be 1 x (N_time + 1). Did you mean to return phi(:, end)?
Related
I'm trying to build a function in Matlab which generates a Taylor series around 0 for sine and the user can insert x (the value for which the sine is approximated) and a maximum error. Thus I want the function to check the maximum error and from this maximum it generates the amount of elements in the Taylor series.
I get the following error:
Error using factorial (line 20)
N must be an array of real non-negative integers.
Error in maxError (line 19)
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
Below my code.
function [n] = maxError(x,e);
%Computes number of iterations needed for a given absolute error.
n=1;
while abs(x)^(n+1)/factorial(n+1) >= e
n = n+1;
end
if mod(n,2) == 0
n=n+1;
end
y=#(x) x;
j=1;
while j<n
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
j=j+1;
end
return
I think I get the error because the factorial function can only take up integers, but the way I see it, I am feeding it an integer. Since j=1; and then gets larger by one per iteration, I don't see how Matlab can perceive this as something else than a integer.
Any help is appreciated.
You are using j as an indexing variable, which is also the complex number in Matlab, and your are forgetting a * multiply.
You can use j as a variable (not recommended!) but when you are putting a number in front of it, Matlab will stil interpret is as the complex number, and not as the variable.
Adding the multiplication symbol will solve the issue, but using i and j as variables will give you these hard to debug errors. If you had used a, the error would have been easier to understand:
>> a=10;
>> 2a+1
2a+1
↑
Error: Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.
I so far have the following
y = log(x);
% Ask user for input values for h and M
% M denotes the number of steps of the algorithm.
h = input('Input value h: ');
M = input('Input value M: ');
%Initialize an MxM matrix
D = zeros(M);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
print(phi);
I obtain the error
Error using symengine (line 58) Index exceeds matrix dimensions.
Error in sym/subsref (line 696)
B = mupadmex('symobj::subsref',A.s,inds{:});
Error in RE (line 12) phi = (1/(2*h)) * (y(x+h) - y(x-h));
First, I believe I should be getting an error message about x not being defined. Second, I have no idea what the matrix dimension error is about. Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
First, I believe I should be getting an error message about x not being defined.
I'm guessing that x is defined, or you would get that error upon the line defining phi. To check whether x is defined, type "who" or "whos".
Second, I have no idea what the matrix dimension error is about.
This is most likely because y is a scalar, x + h is equal to some nonzero integer that is not 1, and you're trying to access y(x + h). For your own edification try setting y equal to a scalar (e.g. y = 5;) and seeing what errors are produced by indexing it in various legitimate and non-legitimate ways (e.g. y(1), y(0), y(3), y(-1), y(1.5)).
Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
Based on the context it looks like you want y to be defined as a function of x instead of a scalar. In other words:
y = #(x)log(x);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
The code runs without error when you change the definitions to the above.
One other error you will run into: the print command is not what you're looking for - this prints a figure to a file. You're probably looking for:
disp(phi);
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.
So I'm just trying to plot 4 different subplots with variations of the increments. So first would be dx=5, then dx=1, dx=0.1 and dx=0.01 from 0<=x<=20.
I tried to this:
%for dx = 5
x = 0:5:20;
fx = 2*pi*x *sin(x^2)
plot(x,fx)
however I get the error inner matrix elements must agree. Then I tried to do this,
x = 0:5:20
fx = (2*pi).*x.*sin(x.^2)
plot(x,fx)
I get a figure, but I'm not entirely sure if this would be the same as what I am trying to do initially. Is this correct?
The initial error arose since two vectors with the same shape cannot be squared (x^2) nor multiplied (x * sin(x^2)). The addition of the . before the * and ^ operators is correct here since that will perform the operation on the individual elements of the vectors. So yes, this is correct.
Also, bit of a more advanced feature, you can use an anonymous function to aid in the expressions:
fx = #(x) 2*pi.*x.*sin(x.^2); % function of x
x = 0:5:20;
plot(x,fx(x));
hold('on');
x = 0:1:20;
plot(x,fx(x));
hold('off');
I implemented the following function in Matlab.
function [x y] = cloud(a,b,phi,x0,y0,N)
phi=phi*2*pi/360;
m=ceil(5*N/pi);
x=a*(-1+2*rand(m,1));
y=b*(-1+2*rand(m,1));
f=sqrt(a^2 - b^2);
indexMatrix=zeros(m,3);
indexMatrix(:,1)=x;
indexMatrix(:,2)=y;
insidePointsMatrix=zeros(1,2);
j=1;
for i=1:m
insidePoint=sqrt(((x(i)+f).^2) + (y(i))^2) + sqrt(((x(i)-f).^2) + (y(i))^2);
if (insidePoint<=2*a)
indexMatrix(i,3)=1;
%insidePointsMatrix(j,:)=indexMatrix(i,1:2);
if j<=N
insidePointsMatrix(j,1:2)=indexMatrix(i,1:2);
j=j+1;
end
end
end
I am reading about arrayfun and cellfun, and I am wondering if it is possible to shorten the function I already implemented using it. Given a 2 x n matrix A where (u,v) are the entries for any given row, how can I return a matrix listing the output of the following formula for each row?
sqrt((u+f).^2+v.^2) + sqrt((u-v)^2+v^2)
The variables f and a are defined above. I am trying to transform a series of points into an ellipse.
using find should give you the u,v coordinates:
cond=sqrt(((x+f).^2) + y.^2) + sqrt(((x-f).^2) + y.^2) ;
ind=find(cond<=2*a);
u=x(ind);
v=y(ind);