1 view (last 30 days)

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

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.

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

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.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/504991-if-statements-keep-getting-error-can-someone-explain-why#comment_796181

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/504991-if-statements-keep-getting-error-can-someone-explain-why#comment_796181

Sign in to comment.