Comsol multiphysics; fzero and inline function with many parameters

Click For Summary
SUMMARY

The discussion focuses on using the fzero function in COMSOL Multiphysics with inline functions that have multiple parameters. The user initially encounters an error when attempting to define an inline function with four parameters. The solution provided involves using an anonymous function to correctly pass the additional parameters to fzero. The corrected implementation is: myfun=inline('exp(y).*cos(x)+z+u', 'x', 'y', 'z', 'u'); x1 = fzero(@(x) myfun(x,-5,2,1,0.3),-5).

PREREQUISITES
  • COMSOL Multiphysics 5.6 or later
  • Understanding of inline functions in MATLAB
  • Familiarity with the fzero function for root finding
  • Basic knowledge of anonymous functions in MATLAB
NEXT STEPS
  • Explore the use of anonymous functions in MATLAB
  • Learn about advanced parameter passing in MATLAB functions
  • Investigate alternative root-finding methods in COMSOL Multiphysics
  • Review the documentation for inline functions in MATLAB
USEFUL FOR

Mathematicians, engineers, and researchers using COMSOL Multiphysics who need to implement complex functions with multiple parameters in their simulations.

hadoque
Messages
39
Reaction score
1
Hi
I'm trying to get comsol multiphysics to first define an inline
function with 1 variable and 4 parameters, and then run fzero on that
same function. I can do it with two parameters, but no moore.

This works:

Code:
clear
myfun=inline('exp(y).*cos(x)+z', 'x', 'y', 'z');
x1 = fzero(myfun,-5,2,1)

This does not work:

Code:
clear
myfun=inline('exp(y).*cos(x)+z+u', 'x', 'y', 'z', 'u');
x1 = fzero(myfun,-5,2,1,0.3)

C» test
Error: Too few arguments to inline function.


Would be thankful for any hints about this.


/Johan
 
Physics news on Phys.org
Hi Johan,
I had a similar issue when trying to use the fzero function in COMSOL Multiphysics. It seems that you need to pass the additional parameter as an anonymous function. Try this:

clear
myfun=inline('exp(y).*cos(x)+z+u', 'x', 'y', 'z', 'u');
x1 = fzero(@(x) myfun(x,-5,2,1,0.3),-5)

Hope this helps!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K