if statements, keep getting error can someone explain why?

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