What is wrong with this matlab code?

In summary, a user-defined MATLAB function was written to calculate the values of y given a mathematical function y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2 and an input value x. The function was designed to handle input vectors and was used to create a plot of the function for the range -3≤x≤4. A suggestion was made to use .* instead of * to correctly handle the vector operations. The author also raised questions about setting the x array within the function and calculating the y values before setting the x array.
  • #1
Firben
145
0
Write a user-defined MATLAB function for the following math function:
y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2
The input to the function is x and the output is y. Write the function such that x can be a vector

Use this function to make a plot of the function y(x) -3≤x≤4

function y=chp7one(x)
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
x=[-3 -2 -1 0 1 2 3 4];
plot(x,y);

Error in ==> chp7one at 2
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
 
Last edited:
Physics news on Phys.org
  • #2
Any idea ? (Urgent)
 
  • #3
I'd try it one term at a time to see which one doesn't like. I'm suspicious of the second term between the exp(...)* x shouldn't that be exp(...).* x

Just a thought I use freemat not matlab.
 
  • #4
Yes, but i still get error messages
 
  • #5
try this, it worked on my version:

y=-0.2.*x.^4+exp(-0.5.*x).*x.^3 + 7.*x.^2

just added a few more dots to the mix
 
  • #6
Firben said:
Write a user-defined MATLAB function for the following math function:
y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2
The input to the function is x and the output is y. Write the function such that x can be a vector

Use this function to make a plot of the function y(x) -3≤x≤4

function y=chp7one(x)
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
x=[-3 -2 -1 0 1 2 3 4];
plot(x,y);

Error in ==> chp7one at 2
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;

I have a couple of questions:
How can you calculate the y values before you set the x array?
Why are you setting x in your chp7one function when it is being passed in as a parameter?
 

FAQ: What is wrong with this matlab code?

1. What is the purpose of this code?

The purpose of this code is not explicitly stated, so it is difficult to determine what it is meant to do. It is important to have a clear understanding of the intended purpose before evaluating the code for errors.

2. Are there any syntax errors?

It is possible that there are syntax errors in the code, which could cause it to not run properly. It is important to carefully check for any missing or incorrect punctuation, parentheses, brackets, etc.

3. What are the expected inputs and outputs?

In order to properly evaluate the code, it is important to know what inputs it expects and what outputs it should produce. This will help determine if the code is functioning correctly or if there are errors in the logic.

4. Is the code efficient?

Efficiency is an important factor in coding, especially in scientific applications where large amounts of data may be involved. It is important to consider if there are any unnecessary or redundant steps in the code that could be optimized.

5. Are there any logical errors?

Even if the code runs without any syntax errors, there may still be logical errors that cause it to produce incorrect results. It is important to carefully review the code and test it with different inputs to identify any potential logical errors.

Similar threads

Replies
10
Views
2K
Replies
1
Views
1K
Replies
7
Views
1K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
6
Views
2K
Back
Top