Parse error at zeta.

조회 수: 11 (최근 30일)
Abdul Mirza
Abdul Mirza 2021년 12월 27일
답변: Image Analyst 2021년 12월 27일
I am trying to solve a differential equation using MATLAB however I am having trouble. there is a red error next to Zeta- the error is as follows:
This statement is not inside any function. Parse error at zeta. usage might be invalid MATLAB syntax
Thank you.
function dydt= odefcn(t,y,f,r,zeta)
dydt=zeros(2,1);
dydt(1)=y(2);
dydt(2)=fsin(r*t)-2.\z.y(2)-sin(y);
end
%parameters and initial conditions
zeta = 0.5;
f = 0.40 + (0.1: 0.1: 0.4);
r = (0.3: 0.3: 1.5);
y(0)=0.5;
dy/dt(0)=0;
tspan = [0 1000]
options = odeset('RelTol',1e-6,'AbsTol' 1e-9 1e-9]);
[t,y] = ode45(@(t,y) odefcn(t,y,f,r,zeta), tspan, y0);
  댓글 수: 3
Abdul Mirza
Abdul Mirza 2021년 12월 27일
Okay I've posted the full code in now.
I haven't tried running the code with a a MATLAB restart.
Abdul Mirza
Abdul Mirza 2021년 12월 27일
Can you please help me with this?

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2021년 12월 27일
There appear to be errors in ‘odefcn’ that need to be addressed.
I have no idea if ‘f’ and ‘r’ are to be evaluated together (as I did here), or if they need to be created using ndgrid as:
[fm,rm] = ndgrid(f,r);
and then evaluated as:
fv = fm(:);
rv = rm(:);
with:
N = numel(fv);
for k = 1:N
[t,y{k}] = ode45(@(t,y) odefcn(t,y,fv(k),rv(k),zeta), tspan, y0);
end
so every value of each is evalueated with evary value of the other, so I defer that to you.
%parameters and initial conditions
zeta = 0.5;
% f = 0.40 + (0.1: 0.1: 0.4)
N = 5;
f = linspace(0.4, 0.8, N);
% r = (0.3: 0.3: 1.5)
r = linspace(1.3, 1.5, N);
% y(0)=0.5;
% dydt(0)=0;
y0 = [0.5, 0];
% tspan = [0 1000];
tspan = linspace(0, 1000, 250);
options = odeset('RelTol',1e-6,'AbsTol', 1e-9);
for k = 1:N
[t,y{k}] = ode45(@(t,y) odefcn(t,y,f(k),r(k),zeta), tspan, y0);
end
figure
for k = 1:N
subplot(N,1,k)
plot(t,y{k})
grid
title(sprintf('f = %.1f, r = %.1f',f(k),r(k)))
legend('$\dot{y_2}$','$y_2$', 'Interpreter','latex', 'Location','bestoutside')
end
function dydt= odefcn(t,y,f,r,zeta)
dydt=zeros(2,1);
dydt(1)=y(2);
dydt(2)=f.*sin(r*t)-2.\zeta.*y(2)-sin(y(1));
end
The code runs without error, however ‘odefcn’ needs to be carefully edited to be certain it is correct.
.

Image Analyst
Image Analyst 2021년 12월 27일
There is no zeroeth element like you try to assign here:
y(0)=0.5;
The first index is 1, not 0.
This is not valid syntax:
dy/dt(0)=0;
not sure what you were thinking there but you might be able to delete the whole line.
This seems to be missing a command and left bracket and maybe some other stuff
options = odeset('RelTol',1e-6,'AbsTol' 1e-9 1e-9]);
Like maybe it should be:
options = odeset('RelTol', 1e-6, 'AbsTol', [1e-9, 1e-9]);
That's a start anyway. Fix those and then there may be other errors but just work your way through them one at a time.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by