Optimization: Wots wrong with my MATLAB CODE

In summary, the conversation is about optimization and a specific problem involving finding the minimum of a function using the Golden-section method. The person is struggling and asks for help, while another person suggests explaining the problem in more detail to get better assistance.
  • #1
mooberrymarz
53
0
Optimization: I am going insane here

:cry: :cry: I've really run out of ideas... please help me.!

%golden.m
function [f,a]=golden(func,p,tol)
func='dfunc';
p=[0 1]
g=0.38;
a=p(1);
b=p(2);
r=b-a
tol=0.01;
iter=0
while r>tol
x=[a+g*r b-g*r]
y=feval(func,x)
if y(1)<y(2)
b=x(2);
else
a=x(1);
end
r=b-a;
iter=iter+1;
end
iter
f=feval (func, a);

%dfunc.mfunction

q=dfunc(x)
q=x'*x-3*x*(exp^-x);
 
Last edited:
Physics news on Phys.org
  • #2
oh yeah, I had to use Golden-section to find the min of f(x)=x^2 - 3xe^-x in the interval [0,1]
......I shalll now open a fortune cookie....

and it says that i am sooooo buggered if u don't help me.
 
Last edited:
  • #3
Why don't you try to actually explain to us what you're trying to do, and why exactly it's not working as you'd like? That sure would help.

- Warren
 

1. How can I improve the speed of my code?

There are a few ways to optimize the speed of your MATLAB code. One approach is to vectorize your code, which means using array operations rather than looping through individual elements. Another way is to preallocate arrays and avoid resizing them during the execution of your code. Additionally, you can use built-in functions or the JIT (Just-In-Time) compiler to improve performance.

2. Why is my code using too much memory?

If your code is using too much memory, it could be due to inefficient memory allocation or large arrays. To optimize memory usage, try preallocating arrays and clearing unnecessary variables from the workspace. You can also use the profiler tool to identify areas of your code that are using a lot of memory.

3. How can I optimize my code for parallel processing?

To optimize your code for parallel processing, you can use built-in functions like 'parfor' or 'spmd' to execute loops in parallel. You can also use the Parallel Computing Toolbox to distribute your code across multiple processors or machines. Additionally, make sure your code is vectorized and avoid using global variables, as they can cause conflicts in parallel processing.

4. What are some common mistakes that can impact the performance of my code?

Some common mistakes that can impact the performance of your code include using inefficient loops, resizing arrays during execution, and using unnecessary functions or operations. It's also important to pay attention to the data types you are using, as using doubles or singles can significantly impact the speed and memory usage of your code.

5. How can I troubleshoot errors in my code?

If you encounter errors in your code, the first step is to carefully read the error message to understand what went wrong. You can also use the debugger tool to step through your code and identify where the error is occurring. Additionally, using the 'try-catch' statement can help you handle and troubleshoot errors in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
106
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
546
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
978
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top