Can someone check my code?

조회 수: 9 (최근 30일)
Bri
Bri 2014년 10월 15일
답변: siham boujrad 2019년 5월 11일
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.
Star Strider
Star Strider 2014년 10월 17일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by