Can someone check my code?

1 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2014-10-15
  7 个评论
Star Strider
Star Strider 2014-10-17
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.

请先登录,再进行评论。

更多回答(2 个)

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

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by