what's wrong with this code. why it show error can anyone tell?

1 次查看(过去 30 天)
function matlab
clc;clear;
%Radioactive decay
y0=[5*10^26;0];
soln = ode23(@f1,[0 8],y0)
t = linspace(0,8,24);
y(:,1)=deval(soln,t,1); %Strontium
y(:,2)=deval(soln,t,2); % Yttrium
figure
plot(t,y(:,1),'-o',t,y(:,2),'--');
hold on;grid on;
legend('Strontium','Yttrium')
end
function dxdt = f1(x,t)
r1 = 0.256;
r2 = 0.127;
dxdt(1) = -r1 * x;
dxdt(2) = -r2 * x;
dxdt =dxdt';
end
function matlab
Error: Function definition not supported in this context. Create functions in code file.

回答(1 个)

Alan Stevens
Alan Stevens 2021-1-10
You probably want something like this (don't enclose it with the statement 'function matlab' - that won't work).
Remember to include the build-up of Ytterbium from Strontium, or ytterbium's curve will stay at zero!).
%Radioactive decay
y0=[5*10^26;0];
soln = ode23(@f1,[0 8],y0);
t = linspace(0,8,24);
y(:,1)=deval(soln,t,1); %Strontium
y(:,2)=deval(soln,t,2); % Yttrium
figure
plot(t,y(:,1),'-o',t,y(:,2),'--');
grid on
legend('Strontium','Ytterbium')
function dydt = f1(~,y)
r1 = 0.256;
r2 = 0.127;
dydt(1) = -r1 * y(1);
dydt(2) = r1*y(1) -r2 * y(2);
dydt =dydt';
end

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by