MATLAB Answers

if statements, keep getting error can someone explain why?

1 view (last 30 days)
Alex Doan
Alex Doan on 12 Feb 2020
Commented: Walter Roberson on 12 Feb 2020
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 Comment

Adam Danz
Adam Danz on 12 Feb 2020
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).

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 12 Feb 2020
Edited: Adam Danz on 12 Feb 2020
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 Comments

Alex Doan
Alex Doan on 12 Feb 2020
i fixed it it still is running error
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
Adam Danz
Adam Danz on 12 Feb 2020
Please supply the full copy-pasted error message (all of it).
Walter Roberson
Walter Roberson on 12 Feb 2020
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

Sign in to comment.


Walter Roberson
Walter Roberson on 12 Feb 2020
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.

Tags

Community Treasure Hunt

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

Start Hunting!