I'm receiving this error: "Incorrect number or types of inputs or outputs for function int." when trying to run the following code. I think because of the symbolic variables

Code:
syms a1 a2 a3 a4 theta
dzdx = @(a1, a2, a3, a4) a1*pi*cos(.5*pi*(1-cos(theta))) + 2*a2*pi*cos(pi*(1-cos(theta))) + 3*a3*pi*cos(1.5*pi*(1-cos(theta))) + 4*a4*pi*cos(2*pi*(1-cos(theta)));
eqn1 = .06981 == (1/pi)* int(@(theta) dzdx(theta), 0, pi);
eqn2 = .19099 == (2/pi) * vpaintegral(@(theta) dzdx*cos(theta), 0, pi);
eqn3 = .19863 == (2/pi) * vpaintegral(@(theta) dzdx*cos(2*theta), 0, pi);
eqn4 = 0 == (2/pi) * vpaintegral(@(theta) dzdx*cos(3*theta), 0, pi);
eqns = [eqn1, eqn2, eqn3, eqn4];
S = vpasolve(eqns, [a1, a2, a3, a4])
I am trying to solve this system of equations that contains both integrals and symbolic variables. I've tried using int, integral, vpaintegral, and using matlabFunction on what is within the integral but nothing is working. I think it might have to do with the fact that int is returing a value with a synbolic variable contained within it. Anyone know how to fix this?
Note: I'm aware that I use vpaintegral and int that's a result of messing around to attempt to get code that works

 Akzeptierte Antwort

syms a1 a2 a3 a4 theta
dzdx = a1*pi*cos(.5*pi*(1-cos(theta))) + 2*a2*pi*cos(pi*(1-cos(theta))) + 3*a3*pi*cos(1.5*pi*(1-cos(theta))) + 4*a4*pi*cos(2*pi*(1-cos(theta)));
eqn1 = .06981 == (1/pi)*int(dzdx,theta,0,pi);
eqn2 = .19099 == (2/pi)*int(dzdx*cos(theta),theta,0,pi);
eqn3 = .19863 == (2/pi)*int(dzdx*cos(2*theta),theta,0,pi);
eqn4 = 0 == (2/pi)*int(dzdx*cos(3*theta),theta, 0,pi);
eqns = [eqn1, eqn2, eqn3, eqn4];
S = vpasolve(eqns, [a1, a2, a3, a4])
S = struct with fields:
a1: 0.049447415580834205495637650479853 a2: 0.014643697825083290473989334878046 a3: 0.0028036567554283219765824766361406 a4: 0.015106840082089730739717245612301

2 Kommentare

Arguably the answer is slightly different than that.
syms a1 a2 a3 a4 theta
Pi = sym(pi);
dzdx = a1*Pi*cos(.5*Pi*(1-cos(theta))) + 2*a2*Pi*cos(Pi*(1-cos(theta))) + 3*a3*Pi*cos(1.5*Pi*(1-cos(theta))) + 4*a4*Pi*cos(2*Pi*(1-cos(theta)));
eqn1 = sym(06981)/sym(10)^5 == (1/Pi)*int(dzdx,theta,0,Pi);
eqn2 = sym(19099)/sym(10)^5 == (2/Pi)*int(dzdx*cos(theta),theta,0,Pi);
eqn3 = sym(19863)/sym(10)^5 == (2/Pi)*int(dzdx*cos(2*theta),theta,0,Pi);
eqn4 = 0 == (2/Pi)*int(dzdx*cos(3*theta),theta, 0,Pi);
eqns = [eqn1, eqn2, eqn3, eqn4];
S = vpasolve(eqns, [a1, a2, a3, a4])
S = struct with fields:
a1: 0.049447415580834208552594866935194 a2: 0.014643697825083291379297675387772 a3: 0.0028036567554283221499112269923545 a4: 0.015106840082089731673658145542382

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hey,
The issue you’re encountering is due to the fact that int and vpaintegral functions in MATLAB are not designed to handle symbolic variables in the way you’re trying to use them.
In your code, 'dzdx' is a function handle that expects numeric inputs, but you’re trying to pass a symbolic variable theta to it. This is causing the error.
Instead, you should define 'dzdx' as a symbolic function of theta, and then use the symbolic 'int' function to perform the integration. Here’s how you can modify your code:
syms a1 a2 a3 a4 theta real
dzdx = a1*pi*cos(.5*pi*(1-cos(theta))) + 2*a2*pi*cos(pi*(1-cos(theta))) + 3*a3*pi*cos(1.5*pi*(1-cos(theta))) + 4*a4*pi*cos(2*pi*(1-cos(theta)));
eqn1 = .06981 == (1/pi)* int(dzdx, theta, 0, pi);
eqn2 = .19099 == (2/pi) * int(dzdx*cos(theta), theta, 0, pi);
eqn3 = .19863 == (2/pi) * int(dzdx*cos(2*theta), theta, 0, pi);
eqn4 = 0 == (2/pi) * int(dzdx*cos(3*theta), theta, 0, pi);
eqns = [eqn1, eqn2, eqn3, eqn4];
S = vpasolve(eqns, [a1, a2, a3, a4]);
This code defines 'dzdx' as a symbolic function of theta, and then uses the symbolic 'int' function to perform the integration. The 'vpasolve' function is then used to solve the system of equations. Note that 'vpasolve' returns a structure, so you might want to extract the solutions into separate variables for further use.
Hope this helps.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by