Nonscalar arrays of function handles are not allowed; use cell arrays instead error, not sure what it means / how to fix it

2 次查看(过去 30 天)
%% Homework 6 544-483
% Problem 1
clc
clear all
close all
%Parameters
global s r b
s = 10;
r = 28;
b = 8/3;
y_int = 5;
x_int = 5;
z_int = 5;
t_init = 0;
t_final = 25;
v_init = [x_int y_int z_int];
tspan = [t_init t_final];
options = odeset('MaxStep',0.01)
%solve
[t,v] = ode45(@problem1func, tspan, v_init,options)
x = v(:,1);
y = v(:,2);
z = v(:,3);
plot(t,x,'rx')
hold on
plot(t,y,'bx')
ploy(t,z,'gx')
xlabel('t')
ylabel('pop')
%% Here's the function I am running ode 45 into
function [dvdt] = problem1func(t,v)
%v[1] = x
%v[2] = y
%v[3] = z
%dx/dt
dvdt(1) = @(x,y) -s*v(1) + s*v(2);
%dy/dt
dvdt(2) = @(x,y,z) r*v(1) - v(2) - v(1)*v(3);
%dz/dt
dvdt(3) = @(x,y,z) -b*z + x*y;
end
%Error message at the end
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
Error in problem1func (line 9)
dvdt(2) = @(x,y,z) r*v(1) - v(2) - v(1)*v(3);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Problem_1 (line 26)
[t,v] = ode45(@problem1func, tspan, v_init,options)
% I've never worked with cells so this error message is confusing to me the purpose of this code is to plot the Lorenz equations. Thanks for any advice

回答(1 个)

James Tursa
James Tursa 2019-4-11
编辑:James Tursa 2019-4-11
You are simply using the wrong syntax for what you really want. It should be something like this instead:
%v[1] = x
%v[2] = y
%v[3] = z
dvdt = zeros(3,1);
%dx/dt
dvdt(1) = -s*v(1) + s*v(2);
%dy/dt
dvdt(2) = r*v(1) - v(2) - v(1)*v(3);
%dz/dt
dvdt(3) = -b*v(3) + v(1)*v(2);

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by