What is wrong with this matlab code?

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a user-defined MATLAB function intended to compute and plot a mathematical expression. Participants are addressing issues related to vector input handling, syntax errors, and the order of operations within the function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant suggests testing each term of the function individually to identify which part causes errors, expressing suspicion about the multiplication between the exponential function and x.
  • Another participant proposes a modification to the original code by adding element-wise multiplication operators (dots) to ensure proper vector operations.
  • A participant questions the logic of calculating y values before defining the x array and criticizes the reassignment of x within the function when it is already passed as a parameter.

Areas of Agreement / Disagreement

There is no consensus on the correct implementation of the function, as participants offer differing suggestions and raise various concerns about the code structure and syntax.

Contextual Notes

Participants express uncertainty regarding the handling of vector inputs and the implications of MATLAB's syntax rules, particularly concerning element-wise operations.

Firben
Messages
141
Reaction score
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
Any idea ? (Urgent)
 
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.
 
Yes, but i still get error messages
 
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
 
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?
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K