We are trying to solve Q1 using ode45 and these are my functions and my error that i am giving, is there any help that yall can do?

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(theta))/(V(theta));
%defining the function work out in terms of theta
dWout = p*dV(theta);
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
%we have 2 equations and 2 unknowns, which is why ODE 45 will generate P
%and Work.
[SL: formatted code as code]
AND THIS IS THE ERROR THAT I KEEP GETTING
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power
is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise
power.
Error in P2Q1Test>V (line 60)
Volume = V0*(1+((r-1)/(2*E))*(1+E*(1-cos(theta))-sqrt(1-E^2*(sin(theta))^2)));
Error in P2Q1Test>dv (line 31)
VolumeDer = diff(V(theta),1);
Error in P2Q1Test>Q1 (line 17)
dV= dv(theta);
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in P2Q1Test (line 7)
ode45(@Q1,t0,y0)
  2 Comments
Star Strider
Star Strider on 1 May 2023
Use element-wise operations.
See the documentation section on Array vs. Matrix Operations for an extended discussion.
That was one problem, fixed by using (.^) instead of (^) in ‘Volume’.
The second problem was that ‘dP’ was returning a matrix rather than the required column vector, and since all but the first column were zeros, just returning the first column solved that.
However the solution seems to be ‘growing’, giving the displayed error. (I initially tried to fix that by using linspace to create an appropriate length initial conditions vector, however the result appears to always be one element greater than the size of the initial conditions vector, so just changing the initial conditions vector didn’t solve that.)
.So, I leave that to you —
t0 = -10:10;
y0 = pi:3*pi;
ode45(@Q1,t0,y0)
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);
% 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
.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 1 May 2023
There are a number of issues with this code. Stepping through:
clc;clear;close all;
You don't need to include these in your script, and calling them every time will impede your ability to debug your code (clear clearing breakpoints, clc erasing the text displayed from previous runs through your code that you may want to compare against the current run.) I'd remove them from your script and only run them manually when you specifically intend to clear the relevant information.
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
Typo, "varibales" should be "variables".
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);
I strongly encourage you not to have two functions and/or variables whose names differ only by case. It would be all too easy to call dv instead of dV or vice versa and those types of bugs can be very difficult to debug. Our brains are trained to "know what I meant" and so skip over issues like tihs sometimes. [Did you catch the typo I left in the previous sentence or did you "know what I meant"?]
In addition, this line calls your dv function then overwrites the dV value passed into your Q1 function by ode45. Did you intend to throw that variable away?
% dV = diff(V(theta),1);
% dv = dV(theta)
%Louis is defining the function dP in terms of theta
dP = gamma*(p*dV(theta))/(V(theta));
%defining the function work out in terms of theta
dWout = p*dV(theta);
end
%setting the function to calculate the derivative of Volume in terms of
%theta.
function VolumeDer = dv(theta)
VolumeDer = diff(V(theta),1);
Know that the diff of a vector is one element shorter than that vector. See x and y in the output I pasted below. This could cause problems later in your code, since ode45 expects the output of the function whose handle you pass into it as the first input to be the same size as the second input with which ode45 calls that function.
% Added to show the behavior of diff, not part of the original code
>> x = 1:10;
>> y = diff(x, 1);
>> whos x y
Name Size Bytes Class Attributes
x 1x10 80 double
y 1x9 72 double
Continuing on
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);
Rather than defining these constants in each of the functions that are being called, I would use one of the techniques described on this documentation page to pass them between functions as additional parameters, possibly packed into a struct array from which each function can extract the fields it cares about and ignore the rest. Your T function, for example, doesn't use the variable gamma.
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)));
theta is a vector. To work with vectors element-wise you want to use array operations, not matrix operations. In this case sin(theta))^2 should be sin(theta)).^2 instead. For scalars like E the array operations behaves the same as the matrix operations, so either E^2 or E.^2 would work. But for non-scalars they can be very different.
There are other places in your functions where you probably want to use array operations instead of matrix operations as well.
end
%we have 2 equations and 2 unknowns, which is why ODE 45 will generate P
%and Work.

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!