i keep getting this error when solving for Q1 in ode45. This is my code and the error. Could anyone fix the mistake or show me what is wrong?

8 views (last 30 days)
clc;clear;close all;
t0 = -3:.1:3;
y0 = [1.013e5 0];
[dV,theta] = ode45(@Q1,t0, y0)
% Louis is creating the function dP. work out,
function [dP,dWout] = Q1(dV,theta)
%setting all known varibales defining each
gamma = 1.4; R = 287; r = 8.4; L = 12; S = 8; V0 = 50; b = 9; T1 = 300;
theta_s = 3*pi/2; theta_b = pi; q_in = 2.8*10^6; Tw = 300; p = 1.013e5;E = 8/(2*12);
dV= dv(theta);
% dV = diff(V(theta),1);
% dv = dV(theta)
%Louis is defining the function dP in terms of theta
dP = gamma*(p*dV)/(V(theta));
dP = dP(:,1);
%defining the function work out in terms of theta
dWout = p*dV;
end
%setting the function to calculate the derivative of Volume in terms of
%theta.
function VolumeDer = dv(theta)
VolumeDer = diff(V(theta),1);
end
%Defining the Temeprature function in terms of theta.
function Tspan = T(theta,C)
gamma = 1.4; R = 287; r = 8.4; L = 12; S = 8; V0 = 50; b = 9; T1 = 300;
theta_s = 3*pi/2; theta_b = pi; q_in = 2.8*10^6; Tw = 300; p = 1.013e5;E = 8/(2*12);
Tspan = (p*V(theta))/((M(C,theta))*(R));
end
%defining our mass function in terms of theta
function mass = M(C,theta)
gamma = 1.4; R = 287; r = 8.4; L = 12; S = 8; V0 = 50; b = 9; T1 = 300;
theta_s = 3*pi/2; theta_b = pi; q_in = 2.8*10^6; Tw = 300; p = 1.013e5; E = 8/(2*12);
mass = M0*exp(-C/w)*(theta-pi);
end
function Volume = V(theta)
gamma = 1.4; R = 287; r = 8.4; L = 12; S = 8; V0 = 50; b = 9; T1 = 300;
theta_s = 3*pi/2; theta_b = pi; q_in = 2.8*10^6; Tw = 300; p = 1.013e5;E = 8/(2*12);
Volume = V0*(1+((r-1)/(2*E))*(1+E*(1-cos(theta))-sqrt(1-E.^2*(sin(theta)).^2)));
end
THIS IS THE ERROR I KEEP GETTING
Q1 returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned
by Q1 and the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in P2Q1Test (line 8)
[dV,theta] = ode45(@Q1,t0, y0)
  1 Comment
James Tursa
James Tursa on 1 May 2023
The error message is pretty self explanatory. Your y0 has two elements, but your Q1 derivative function only returns one element. I was going to suggest something like this is maybe what you meant:
function result = Q1(dV,theta)
:
result = [dP;dWout];
end
but other stuff doesn't look right in your derivative code. E.g., Why is dV listed as the first argument in your derivative function and not t? And why, since this is an input from the ode45( ) integrator, are you redefining this inside your derivative function?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 1 May 2023
You are trying to use numeric diff() to do calculas differentiation.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!