Can someone check my code?

1 vue (au cours des 30 derniers jours)
Bri
Bri le 15 Oct 2014
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

Réponse acceptée

Star Strider
Star Strider le 15 Oct 2014
  7 commentaires
Star Strider
Star Strider le 17 Oct 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 le 17 Oct 2014
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (2)

siham boujrad
siham boujrad le 11 Mai 2019
%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 le 11 Mai 2019
plz i need someone to check the code for me. Thank you

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by