Efficient MATLAB M-File for Gradient Approximation with Newton's Method

  • MATLAB
  • Thread starter qspeechc
  • Start date
  • Tags
    Matlab
In summary: It works if, when you call g you do it as such:g(inline('x^2'), 2)You should make h a global variable so g can access it without calculating it every time.
  • #1
qspeechc
844
15
I have this assignment:
https://vula.uct.ac.za/access/content/group/317a243a-d926-4470-804c-7d46e296ab63/ass3_07.pdf

Although, I don't know if you'll be able to access that.
I am busy with task 2 in the file above.
Anyway, I am trying to write an M-File for a function g. g has two input arguments, a function f(x), and a x-value of the function. The file is then meant to use Newton's Method to approximate the gradient at x.

Here is my code:

>>function y = g(f(x), x)
>>y = (f(x+h) - f(x))/h;

I have found the best value for h in a previous bit of code.

In the command window I try to evaluate a functions' gradient at a point thus:

>>g(x^2, 2)

Meaning f(x) = x^2 and x = 2, but I get the error:
? Error: File: F:\My Documents\MATLAB\Numerical Methods\g.m Line: 1 Column: 17
Unbalanced or misused parentheses or brackets.

The same for
>>g(x.^2, 2)
>>g('x^2', 2)
>>g('x.^2', 2)

But I can not see where I have "Unbalanced or misused parentheses or brackets" in the line:

>>function y = g(f(x), x)

What is my error, and how do I correct it? Thanks.
 
Physics news on Phys.org
  • #2
qspeechc said:
I have this assignment:
https://vula.uct.ac.za/access/content/group/317a243a-d926-4470-804c-7d46e296ab63/ass3_07.pdf

Although, I don't know if you'll be able to access that.
I am busy with task 2 in the file above.
Anyway, I am trying to write an M-File for a function g. g has two input arguments, a function f(x), and a x-value of the function. The file is then meant to use Newton's Method to approximate the gradient at x.

Here is my code:

>>function y = g(f(x), x)
>>y = (f(x+h) - f(x))/h;

I have found the best value for h in a previous bit of code.

In the command window I try to evaluate a functions' gradient at a point thus:

>>g(x^2, 2)

Meaning f(x) = x^2 and x = 2, but I get the error:
? Error: File: F:\My Documents\MATLAB\Numerical Methods\g.m Line: 1 Column: 17
Unbalanced or misused parentheses or brackets.

The same for
>>g(x.^2, 2)
>>g('x^2', 2)
>>g('x.^2', 2)

But I can not see where I have "Unbalanced or misused parentheses or brackets" in the line:

>>function y = g(f(x), x)

What is my error, and how do I correct it? Thanks.

You should make
function y = g(f,x,h)
y = (f(x+h) - f(x))/h;

and a function f:
function y = f(x)
y = x^2;

And call

g(@f,2,h)
 
  • #3
It works if, when you call g you do it as such:

g(inline('x^2'), 2)

You should make
function y = g(f,x,h)
h is not an input variable, it is calculated according to the computer's computing power (?)

And call

g(@f,2,h)

Sorry, I do not get that.

Anyway, it doesn't really matter because I found a way I understand to make it work. I would like to get rid of having to write "inline" though.
Thank you for your help.
 
  • #4
qspeechc said:
It works if, when you call g you do it as such:

g(inline('x^2'), 2)


h is not an input variable, it is calculated according to the computer's computing power (?)



Sorry, I do not get that.

Anyway, it doesn't really matter because I found a way I understand to make it work. I would like to get rid of having to write "inline" though.
Thank you for your help.
Unless you declare h to be global, there is no means for your function g to know its value, unless you calculate it inside your g function.
The symbol @ is a handle to a function. If you have a function called myfun, you pass it as a parameter for another function using the handle.
for instance:
foo(@myfun, x)
 
  • #5
Yes, h is calculated inside the function file.
We have not covered the topic of handles. I thinks it's best I stick to inline objects, which we have covered, as I do not fully understand handles yet. Maybe I try again tomorrow.
Thank you very much for your help CEL!
 

1. What is MATLAB?

MATLAB is a programming language and interactive computing environment commonly used in scientific and engineering applications. It allows users to perform data analysis, create algorithms, and develop models and simulations.

2. What are M-Files in MATLAB?

M-Files are files written in the MATLAB programming language. They can contain code, functions, and scripts that can be executed within the MATLAB environment. They are commonly used for creating and organizing programs and algorithms.

3. How do I create an M-File in MATLAB?

To create an M-File in MATLAB, go to the "File" menu and select "New". Then, choose "Script" or "Function" depending on the type of M-File you want to create. You can also use the shortcut keys Ctrl+N (Windows) or Command+N (Mac).

4. How do I run an M-File in MATLAB?

To run an M-File in MATLAB, make sure the file is open in the editor. Then, click on the green "Run" button in the toolbar or use the shortcut keys F5 (Windows) or Command+Enter (Mac). Alternatively, you can type the name of the M-File in the command window and press Enter to run it.

5. Can I share my M-Files with others?

Yes, you can share your M-Files with others by sending them the file or by using a version control system such as GitHub. You can also create an executable file from your M-File that can be run on computers without MATLAB installed.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
978
  • Calculus and Beyond Homework Help
Replies
8
Views
442
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
794
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
925
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top