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

I have a problem with my current Matlab code
if true
% code
end
if true
Nt=T/dt+1;
t=0:dt:T;
phi=zeros(Nt,1);
b=zeros(Nt,1);
f=zeros(Nt,1);
p(1)=3;
b(1)=10;
f(1)=l;
for i=1:Nt-1
b(i)=(12*pi(i))/(2*r)^2;
f(i)=l*p(i)^(3/2)/(2*r^2*((p(i)-1)^(1/2)));
p(i+1)=p(i)+dt*(p(i)-p(i)*((1-f/(sqrt(b*p(i))*x))*sinh(sqrt(b*p(i))*x)));
end
For p(i+1) Matlab keeps saying "In an assignment A(I) = B, the number of elements in B and I must be the same." Any ideas?

4 Comments

You should probably presize p rather than have it grow in the loop.
Put a breakpoint in on the line giving the error and put:
size( p(i)+dt*(p(i)-p(i)*((1-f/(sqrt(b*p(i))*x))*sinh(sqrt(b*p(i))*x))) )
on the command line at that point. From a quick glance at your code without time to run it this must be
ans =
1 1
in order for it to be able to be assigned to p(i+1).
it says
if true
ans =
601 1
end
it looks like the problem is the variable f. if i put a fixed value for f the code works. but i don't really know what to do.
You can use syntax like:
p(i+1,:) = ...
to enter values into a 2d array whose second dimension can be 601, but this only works if every set of results being entered into the array has size 601 which is not the case because you assign:
p(1) = 3;
at the start and I don't know if your results are the same size every time round the loop either.
if true
clear all
clc
l=1;
r=1*10^-6;
x=2;
dt=0.005;
T=3;
Nt=T/dt+1;
t=0:dt:T;
p=zeros(Nt,1);
b=zeros(Nt,1);
f=zeros(Nt,1);
p(1)=3;
b(1)=10;
f(1)=l;
for i=1:Nt-1
b(i)=(12*p(i))/(2*r)^2;
f(i)=l*p(i)^(3/2)/(2*r^2*((p(i)-1)^(1/2)));
p(i+1)=p(i)+dt*(p(i)-p(i)*((1-f/(sqrt(b*p(i))*x))*sinh(sqrt(b*p(i))*x)));
end

Sign in to comment.

 Accepted Answer

Nt=T/dt+1;
t=0:dt:T;
phi=zeros(Nt,1);
b=zeros(Nt,1);
f=zeros(Nt,1);
p(1)=3;
b(1)=10;
f(1)=l;
for i=1:Nt-1
b(i)=(12*p(i))/(2*r)^2;
f(i)=l*p(i)^(3/2)/(2*r^2*((p(i)-1)^(1/2)));
p(i+1)=p(i)+dt*(p(i)-p(i)*((1-f(i)/(sqrt(b*p(i))*x))*sinh(sqrt(b*p(i))*x)));
end

5 Comments

Unfortunatelly this didn't solve the problem
That matlab keeps saying "In an assignment A(I) = B, the number of elements in B and I must be the same." If i just replace the f by f(i) this will pop up "Warning: Rank deficient, rank = 0, tol = NaN. "
We can't test your code if we don't know what is T, and other parameters

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 1 Jul 2015

Commented:

on 1 Jul 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!