필터 지우기
필터 지우기

if statements, keep getting error can someone explain why?

조회 수: 2 (최근 30일)
Alex Doan
Alex Doan 2020년 2월 12일
댓글: Walter Roberson 2020년 2월 12일
Promt - Write a user-defined function named Deflection_cougarnetID.m which has one input (position x) and one output, (deflection v). You will need to apply the deflection equation above with the piecewise conditions described. Use this function to determine the deflection at the position entered. Produce a formatted output to the command window that states the deflection at the point entered.
code
script- %%
choose_in = input('Please enter a psoition between 0 and 360 ');
if (choose_in < 0)
choose_in = abs(choose_in);
Warning('You entered a negative value, taking the ablsolute value');
end
if (choose_in > 360)
error('The value is greater than 360 inches, program terminated')
end
Deflection = Deflection_ahdoan2(choose_in)
function -
function [Deflection] = Deflection_ahdoan2(choose_in)
%DEFLECTION_AHDOAN2 Summary of this function goes here
% Detailed explanation goes here
if (0<choose_in<120);
first = (choose_in);
if (120<choose_in<240)
second = (choose_in-120)
else
second = 0
if
(240<choose_in<360)
third = (choose_in-240)
else
third = 0
end
end
end
def_form = (1/3.19*10^9)*(800*choose_in^3)-(13.68*10^6*choose_in)- (2.5*choose_in^4) + 2.5*(second)^4 + 600*third^3))
end
  댓글 수: 1
Adam Danz
Adam Danz 2020년 2월 12일
One of the reasons it's difficult to see the error is because your code is not properly indented. Open this file in matlab's editor, select all of the code (ctrl + a) and smart-indent (ctrl+i).

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

답변 (2개)

Adam Danz
Adam Danz 2020년 2월 12일
편집: Adam Danz 2020년 2월 12일
if you open this file in the editor you'll see red markings indicating the lines that contains an error. The orange lines indicate potential improvements.
When you run the code, the error message tells you the line that contains the error and what the problem is. The error message in r2019b is
Error: File: xxxxxx.m Line: ## Column: ##
Invalid expression. Check for missing or extra characters.
Check out the examples of if-statements in the documentation and you'll see how your code differs from the examples. It's an easy fix.
  댓글 수: 3
Adam Danz
Adam Danz 2020년 2월 12일
Please supply the full copy-pasted error message (all of it).
Walter Roberson
Walter Roberson 2020년 2월 12일
You fixed it incorrectly. We were telling you how matlab was interpreting your existing code, pointing out how it was wrong. You need to rewrite the form
A<X<B
as
A<X && X<B

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


Walter Roberson
Walter Roberson 2020년 2월 12일
if (0<choose_in<120);
In MATLAB, that means
if ((0<choose_in)<120);
0<choose_in is going to be 0 (false) or 1 (true). Then both 0 and 1 are < 120, so this statement would always be true.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by