376 views (last 30 days)
Show older comments
Collin Lightfoot on 5 Mar 2024
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/2090721-i-m-receiving-this-error-incorrect-number-or-types-of-inputs-or-outputs-for-function-int-when-tr
Commented: Walter Roberson on 5 Mar 2024
Accepted Answer: Torsten
Open in MATLAB Online
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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Torsten on 5 Mar 2024
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
Collin Lightfoot on 5 Mar 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2090721-i-m-receiving-this-error-incorrect-number-or-types-of-inputs-or-outputs-for-function-int-when-tr#comment_3089331
Thanks!
Walter Roberson on 5 Mar 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2090721-i-m-receiving-this-error-incorrect-number-or-types-of-inputs-or-outputs-for-function-int-when-tr#comment_3089611
Open in MATLAB Online
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
Sign in to comment.
More Answers (1)
Manikanta Aditya on 5 Mar 2024
Moved: Torsten on 5 Mar 2024
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Collin Lightfoot on 5 Mar 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2090721-i-m-receiving-this-error-incorrect-number-or-types-of-inputs-or-outputs-for-function-int-when-tr#comment_3089336
Thank you!
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxMathematicsCalculus
Find more on Calculus 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office