solve nonlinear equation

I write a method to solve any equation
function [ result ] = get( func )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
result=solve(func);
end
when I run it on command window,I get an error.
>> f=@(x) 2*x+1
f =
@(x)2*x+1
>> [ result ] = get( f )
??? Error using ==> error
Function is not defined for 'function_handle' inputs.
Error in ==> solve>getEqns at 182
error('symbolic:solve:errmsg1', ...
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Error in ==> get at 4
result=solve(func);
so how can I fix the error

Réponses (3)

Oleg Komarov
Oleg Komarov le 31 Mai 2011

0 votes

You cannot solve a function handle.
You have to convert the anonymous function handle to a string function:
fh = @(x,y) 2*x+1-2*y;
% Convert to string
fs = func2str(fh);
% Extract only function part w/o @(.)
fs = regexprep(fs,'@\([\w,]+\)','');
% Solve
solve(fs)

4 commentaires

Walter Roberson
Walter Roberson le 1 Juin 2011
- Be careful about which variable is being solved for
- func2str() only going to do a literal string conversion. If any of the variables involved have "captured values", then the captured value will not have its value substituted by func2str()
- searching for the first ')' and deleting from the beginning to there would probably be more efficient than regexprep()
Oleg Komarov
Oleg Komarov le 1 Juin 2011
The snippet is just un unrefined example, thus point 1 wasn't addressed in the example explicitly.
For point 2 I agree it becomes overly complicated (functions, workspace...) for a problem that can be stated as symbolic from the beginning
For point 3 premature optimization is the... :D (just wanted to write a one liner)
Eman Ahmed Elsayed
Eman Ahmed Elsayed le 1 Juin 2011
func2str doesn't work with me
>> f=@(x) 2*x+1
f =
@(x)2*x+1
>> func2str(f)
ans =
@(x)2*x+1
It's return the same equation
Matt Fig
Matt Fig le 1 Juin 2011
Read the rest of the post. FUNC2STR is working just like Oleg showed...

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 1 Juin 2011

0 votes

solve() only applies to symbolic expressions. If you are going to use symbolic expressions you might as well do so from the start and save the trouble about constructing function handles.

4 commentaires

Oleg Komarov
Oleg Komarov le 1 Juin 2011
Totally agree
Eman Ahmed Elsayed
Eman Ahmed Elsayed le 1 Juin 2011
unfourtanetly, I must use handle function
Walter Roberson
Walter Roberson le 1 Juin 2011
If this is for an assignment, then it would help us to read the assignment question, as then we would not suggest things you cannot use.
Walter Roberson
Walter Roberson le 1 Juin 2011
If the assignment prohibits you from passing around symbolic variables, then why does it permit you to use solve(), the symbolic equation solver?

Connectez-vous pour commenter.

Eman Ahmed Elsayed
Eman Ahmed Elsayed le 1 Juin 2011

0 votes

I'm still have the same problem

1 commentaire

Oleg Komarov
Oleg Komarov le 1 Juin 2011
What do you mean you have the same problem? Post the whole code you're using and the error message.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by