(Should be) Simple function declarations in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter villiami
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around function declarations in Matlab, specifically how to create a function that utilizes previously defined variables while allowing for optimization with respect to one of the variables. Participants explore different methods for defining such functions and the challenges encountered in evaluating them.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to declare a function using inline syntax but encounters errors due to undefined variables a and b when evaluating the function.
  • Another participant suggests using symbolic variables with the 'syms' command, but notes that this approach does not allow for direct function evaluation with specific inputs, which is necessary for optimization.
  • A participant reiterates the need to evaluate the function at specific points (e.g., f(2)) for optimization purposes, expressing frustration with the limitations of the previous suggestions.
  • Further discussion includes a method for defining a function that can be differentiated and solved, but it remains unclear how to incorporate the constants a and b effectively.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to define the function while maintaining the ability to optimize with respect to one variable. There is no consensus on a single effective method, and the discussion remains unresolved.

Contextual Notes

Limitations include the inability to evaluate functions with symbolic variables directly for specific inputs and the challenge of maintaining flexibility with variable values during optimization.

villiami
Messages
27
Reaction score
0
I have already declared some variable values as (for example):
a = 2
b = 3

Now I want to declare a function like:
f(x) = x + a - b
I will be optimizing w.r.t. x later, so I don't want to enter it as f(x,a,b)
(or is it possible to oiptimise w.r.t. just one of the variables?)


I tried
f = inline('x+a-b','x')
but then when I tried to evaluate it, it didn't have values for a and b (even though they were declared before!), so caused an error.

Any ideas? (other than defining the function with a and b replaced by value, because this won't allow me to change a and b later)
 
Physics news on Phys.org
try using

>> syms x,syms a,syms b;
>> f = x-a+b;
>> a = 5, b=6;

a =

5

>> eval(f)

ans =

x+1
 
Thanks for that, but it still doesn't let me type something like:
f(2)

which is what I need for the optimization (where I will enter fminsearch(@f,[0]) to find the minimizing x-value given the constant a and b values)

Any ideas?
 
villiami said:
I have already declared some variable values as (for example):
a = 2
b = 3

Now I want to declare a function like:
f(x) = x + a - b
I will be optimizing w.r.t. x later, so I don't want to enter it as f(x,a,b)
(or is it possible to oiptimise w.r.t. just one of the variables?)


I tried
f = inline('x+a-b','x')
but then when I tried to evaluate it, it didn't have values for a and b (even though they were declared before!), so caused an error.

Any ideas? (other than defining the function with a and b replaced by value, because this won't allow me to change a and b later)

Pretty much this precise problem is solved by carefully reading the output of

Code:
doc fminsearch
 
villiami said:
Thanks for that, but it still doesn't let me type something like:
f(2)

which is what I need for the optimization (where I will enter fminsearch(@f,[0]) to find the minimizing x-value given the constant a and b values)

Any ideas?

I most work with variables, I use following method for optimization:

%continuing from that code ...
>> f = x^2+x+a-b^2 %declaring f again to get some good differentiated function

f =

x^2+x-31


>> diff_f = diff(eval(f),x)

diff_f =

2*x+1


>> solve(diff_f)

ans =

-1/2
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
7
Views
9K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K