I am still not able to understand what I am doing wrong !!
My problem is to make 2 plots x1(t) and x2(t) vs t, and x1(t) vs x2(t).
I have x1(0) = x2(0) = 1, my time span is 0 to 140 seconds and
My 2 non linear differential equations are:
My input u is:
My code is:
clcclear allclose alla = 2;b = 3;k = 1;% input function
u = @(x1, x2) -(x1*tanh(x1) + k*x2 + k*a*x1 + k*x1^2 + (a^2)*(x1)*tanh(x1) + a*x2*tanh(x1) + 2*a*(x1^2)*tanh(x1) + 2*x1*x2*tanh(x1) + b*x1*x2)*(1+x2^2);tspan = [0 140];x0 = [1, 1]; % Initial Condition
[t,x] = ode45(@(x1, x2) odefcn(x1, x2, u, a, b), tspan, x0);%Plots
figureplot(t,x(:,1),'-r');grid on;title('State Response x1(t) and x2(t) w.r.t time');xaxis('time(s)');yaxis('x1(t) and x2(t) in m');hold on;plot(t,x(:,2),'-b');legend('x1(t)', 'x2(t)')hold offfigureplot(x(:,1), x(:,2), '-b');grid on;title('State Response x1(t) vs x2(t)');xaxis('x1(t) in m');yaxis('x2(t) in m');% Function Handle
function dxdt = odefcn(x1, x2, u, a, b)dxdt = zeros(2,1);dxdt(1) = (a*x1 + x2)*tanh(x1);dxdt(2) = b*x1*x2 + (1/(1 +x2^2))*u;end
Error I am getting is:
Unable to perform assignment because the left and right sides have a different number of elements.Error in NLST_HW5_3>odefcn (line 37)dxdt(1) = (a*x1 + x2)*tanh(x1);Error in NLST_HW5_3>@(x1,x2)odefcn(x1,x2,u,a,b)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 NLST_HW5_3 (line 12)[t,x] = ode45(@(x1, x2) odefcn(x1, x2, u, a, b), tspan, x0);
I do not understand why the left and right hand sides have different elements since I am just inputing single value into dxdt(1) and dxdt(2), there are no matrices or anything being used.
I would be grateful if anyone can explain me the reason for error and also help solve the problem !!
Best Answer