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

1 次查看(过去 30 天)
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 个评论
Adam
Adam 2015-7-1
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.
butz]
butz] 2015-7-1
编辑:butz] 2015-7-1
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

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-1
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by