We are trying to use ode45 to solve for function Q1, this is my code and the error that i keep getting. Can i please get some help on what to fix?

1 view (last 30 days)
clc;clear;close all;
t0 = -10:10;
y0 = pi:3*pi;
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 THAT I KEEP GETTING
Error using odearguments
Q1 returns a vector of length 6, but the length of initial conditions vector is 7. 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)
ode45(@Q1,t0,y0)

Answers (1)

Walter Roberson
Walter Roberson on 1 May 2023
y0 = pi:3*pi
y0 = 1×7
3.1416 4.1416 5.1416 6.1416 7.1416 8.1416 9.1416
7 initial conditions
dV= dv(theta);
That are passed to dv
VolumeDer = diff(V(theta),1);
which uses the diff() operation on them. The diff() operation is numeric differences -- x(2:end)-x(1:end-1) . The diff() operation is not derivatives -- not unless V(theta) returns a symbolic results or symbolic function result.
The size of the output for numeric diff() is one smaller in the dimension being operated on (unless second or more differences are requested) so with the input being length 7, the output is length 6.
And then you use that vector of length 6 to calculate the results of the function -- so you are going to return a vector of length 6 when you need a vector of length 7.
  2 Comments
Walter Roberson
Walter Roberson on 1 May 2023
syms theta
dv = simplify(diff(V(theta), theta))
dv = 
That should be your dv, not numeric differences.
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
Walter Roberson
Walter Roberson on 1 May 2023
I recommend that you set up your system using Symbolic Toolbox, and that you then follow the workflow shown in the first example for odeFunction in order to create a function handle to be passed to ode45()

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!