## anonymous 5 years ago Matlab: I have written about 40 programs so far for this one class. I have come across a problem I'm having troubles seeing through clearly or even how to write it. It is using a function to compute the derivative (not the derivative function that may already be in the matlab library, but a fully written out one)

1. anonymous

the equation is$f \prime(x _{i}) =[ f(x _{i+1}) - f(x _{i})]/ \Delta x$ where $\Delta x = x _{i+1} - x _{i}$

2. anonymous

here is a start my teach provided %defining the function for problem 5.24 function dy = deriv1(xmin, xmax, step, f) %last param f is function %write a loop to go through the values of x %and calculate dydx(i) = ... f(x(i) + step) - f(x(i))... % calling function f end

When does it end? Which is to say, do you have a definition for $$x_i$$ for some value of i?

4. anonymous

I don't understand your problem, do you have Calc I experience? The function is rather straight forward.

5. anonymous

yeah i have calc i and ii so far. I am learning matlab so this seems very complicated to write in matlab though i havent tried the problem yet. it seems like a fun one and thought i would throw it out there. right now im working on writing a function that calculates the mins and maxes of a function on a range of x values. so i havent had time to try to solve this one

6. anonymous

Well, the function can be found on wikipedia, ie f'(a) = lim (f(a+h) - f(a)) / h So just figure out f'(a) for each point from xmin to xmax stepping by step. Is your problem finding f'(a)? Or is the problem with the stepping?

7. anonymous

I haven't really looked at this one yet. I have been stuck on this one program for the last 3 classes. I thought I would be able to start this one a lot sooner. So I think this derivative problem will be easier after I figure out this problem. Maybe you could look at it? Its to find the min and max values of any arbitrary function. it has four inputs and four out puts. but I keep getting an error "??? Undefined function or variable 'x'." my program is this and it is wrong because of the error. I know something essential has to be missing. I am stuck on how to take an arbitrary function and use it in my function.

8. anonymous

%inline function must be used in place of func. The inline function %requires a string input. function [xmin, min_value, xmax, max_value] = minmax(first_value, last_value, num_steps, func) step_size = (last_value - first_value)/num_steps x = first_value:step_size:last_value [i_min min_value] = min(func(x)) [i_max max_value] = max(func(x)) xmin = x(i_min); xmax = x(i_max); end

9. anonymous

I'll take a look at it in a few hours. I'm at school at the moment waiting for a couple of students to turn in assignments so that I can finish the grading up for the semester. I'll fire up matlab when I get home, I'm fairly rusty atm.

10. anonymous

Good luck lando! I do know now that this code does work, i got this far. % to call function: [a b c d] = minmaxa(1,2,3, @func) %function [xmin, min_value, xmax, max_value] = %minmaxa(first_value, last_value, num_steps, f) step_size = (last_value - first_value)/(num_steps-1); x = first_value:step_size:last_value; y = f(x); [cmin index] = min(y) min_value = cmin xmin = x(index) [cmax index] = max(y) max_value = cmax xmax = x(index) end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% But i could only get it to work for sin. I do note in the top to call the function you can write exactly that but instead of func, you use something in terms of x like: x.^2 + 2*x The @ is the "function handle" and has to be used. give it a try with the sin func. copy and paste this for the round brackets (0,2pi,100,@sin) kinda cool. the min should be -1 max should be 1. My problem is getting some other function like the x.^2 + 2*x I was going to try to write a separate file to do it.