Can someone check my code?

I am suppose to create a function m file called myminimum(f,a,b) which takes three inputs:
f: A function handle.
a: A real number.
b: A real number.
Note: You may assume that f(x) is a quadratic function and that a < b.
Does: Finds the minimum value of f(x) on [a, b] keeping in mind that this may occur either at the
critical point (if the critical point is in [a, b]) or at one of the endpoints. You’ll probably
want to use some if statements to check cases.
Returns: The minimum value of f(x) on [a, b].
Here is my code:
function m=myminimum (f,a,b);
syms x;
y=subs(f(x),a);
z=subs(f(x),b);
w=subs(diff(f(x)),0);
m=min(y,z,w);
end
It keeps returning an error. The error says "MIN with two matrices to compare and a working dimension is not supported." Can someone tell me how to fix my code?
Here is some sample data and the correct answers to the same code.
a= myminimum(@(x) x^2+1,-3,2)
a = 1
a = myminimum(@(x) x^2+6*x-3,-2,0)
a = -11
a = myminimum(@(x) -2*x^2+10*x,3,8)
a = -48

 Respuesta aceptada

Star Strider
Star Strider el 15 de Oct. de 2014

0 votos

7 comentarios

Bri
Bri el 15 de Oct. de 2014
Not to be rude, but I was reading the comments on that question and just wanted to know if you modified the code at all you posted originally on that questions?
Star Strider
Star Strider el 15 de Oct. de 2014
I haven’t modified it since I posted the Answer (and had a brief conversation with your professor).
Note that the Original Poster of that Question didn’t mention that you have to determine the minimum on the open interval [a,b].
So you need to do both:
  • Determine the value of f(x) at the critical point and the limits of the interval,
  • Determine that the critical point is on the interval, since if it is not, do not consider the value of f(x) at the critical point in determining the minimum.
So the only part of your code you still need to do is to determine that the critical point is on the interval, and then output (as m) the minimum of f(x) on the interval. Note that those tests are not part of my posted code, because your classmate didn’t mention they were necessary.
In the code you posted, you still need to define the derivative, solve for the inflection point, and find f(x) at the inflection point before you evaluate f(x) at the interval limits:
syms x
f = sym(f);
df = diff(f);
cp = solve(df);
w = subs(f,cp);
After that, you need to determine if ‘cp’ is on the interval. If it is, take the minimum of all three points: f(a), f(b), f(cp), else consider f(a) and f(b) only.
Bri
Bri el 17 de Oct. de 2014
Can you please check my code now? It will not return a value when I run
(@(x) 1*x^2+7*x+8, -0.200000, 6.800000)
Can you tell me what I did wrong now?
function m=myminimum (f,a,b);
syms x;
y=subs(f(x),a);
z=subs(f(x),b);
derv=diff(f(x);
c=solve(c==0,x);
w=subs(f(x),c);
if(c>=a & c<=b & w<y & w<z);
m=vpa(w);
elseif (y<z && y<w);
m=vpa(y);
elseif(z<y && z<w);
m=vpa(z);
end
end
Star Strider
Star Strider el 17 de Oct. de 2014
I put the extra vpa calls in for my convenience. You can take them out if you wish.
The problem is that once the first (initial) if condition is satisfied, you can break out of the block because you know the critical point is on the interval and the value of the function there is the minimum. After that, all you need to do is to test to see if y<z.
This works:
syms x;
y=vpa(subs(f(x),a));
z=vpa(subs(f(x),b));
derv=diff(f(x));
c=vpa(solve(derv==0,x));
w=vpa(subs(f(x),c));
if(c>=a && c<=b && w<y && w<z);
m=vpa(w);
break
elseif (y<z);
m=vpa(y);
else
m=vpa(z);
end
Bri
Bri el 17 de Oct. de 2014
Thank you so much, you have been very helpful throughout the duration of my matlab class. I really appreciate it.
Star Strider
Star Strider el 17 de Oct. de 2014
I took the essence of your code outside of the function (exactly as I posted it) because I run everything I test on MATLAB Answers in a test script file that doesn’t allow function definitions, so break worked. Replace break with return and all should be well.
Star Strider
Star Strider el 17 de Oct. de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (2)

siham boujrad
siham boujrad el 11 de Mayo de 2019

0 votos

%This program computes to calculate the Total Resistance (or Impedance) for Series or Parallel Circuit.
R1=input('Enter the value of R1'); %ohms
R2=input('Enter the value of R2'); %ohms
R3=input('Enter the value of R3'); %ohms
R=[R1,R2,R3];
RTs=sum(R);
RTp=1/sum(1./R);
If RTs>=5 & RTs<25
fprintf('Resistors are in series and R_Total = %7.2f ohms \n',RTs)
elseif RTp>0 & RTp<=1
fprintf('Resistors are in parallel and R_Total = %7.2f ohms \n',RTp)
else disp('Error')
endif
siham boujrad
siham boujrad el 11 de Mayo de 2019

0 votos

plz i need someone to check the code for me. Thank you

Etiquetas

Preguntada:

Bri
el 15 de Oct. de 2014

Respondida:

el 11 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by