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
Ältere Kommentare anzeigen
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
Weitere Antworten (1)
Manikanta Aditya
am 5 Mär. 2024
Verschoben: Torsten
am 5 Mär. 2024
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.
1 Kommentar
Collin Lightfoot
am 5 Mär. 2024
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!