Why won't my Matlab code work?

In summary, the author of the code corrected the mistake of v(i) depending on v(i-1) rather than v(i+1) depending on v(i).
  • #1
grandpa2390
474
14

Homework Statement


I need to write a program in Matlab that accomplishes this graph:

?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png

I keep getting an error on Line31 Column 161 about unexpected parenthesis. They look balanced to me. If I take one away from either side, I get an error that I am short a parenthesis.

I also need to change the colors of the lines and position the titles in the proper place by the lines.

Homework Equations


?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png


The Attempt at a Solution


Matlab:
close all

clear all

clc
%%defining variables

tmin = 0; %(in seconds)

tmax = 200; %(in seconds)

dt = .1;

vmin = 1; % in m/s

power = 400; % in Watts

mass = 70; % in kg

dragCoefficient = .5

airDensity = 1.225 %kg/m^3

surfaceArea = .33 %in Meters Squared
%%Velocity Without Drag

t = tmin:dt:tmax; % time steps

[j,k] = size(t);

velocityWithoutDrag(1) = vmin;

% calculate instantenous speeds

for i = 2:k

velocityWithoutDrag(i) = velocityWithoutDrag(i-1) + (power.*dt)./(mass.*velocityWithoutDrag(i-1));

end
%%Velocity With Drag

t = tmin:dt:tmax; % time steps

[j,k] = size(t);

velocityWithDrag(1) = vmin;

% calculate instantenous speeds

for i = 2:k

velocityWithDrag(i) = velocityWithDrag(i) + ((power.*dt)./(mass.*velocityWithDrag(i)))-((dragCoefficient.*airDensity.*surfaceArea.*velocityWithDrag(i)^2)/(mass.));

end
%%Plots

figure(1)

hold on

box on

plot(t,velocityWithoutDrag,t,velocityWithDrag)

set(gca,'XScale','lin','YScale','lin')

set(gcf,'Color','w');

set(gcf,'Resize','on');

ylabel('V (m/s)')

xlabel ('time (s)')

text(t(50),velocityWithoutDrag(50),'no air resistance')

title('Bicycling without air resistance')

hold off
<Moderator's note: please use code tags>

I'm sorry. I didn't know about the code tag :(
 

Attachments

  • Screen Shot 2018-03-02 at 9.41.58 AM.png
    Screen Shot 2018-03-02 at 9.41.58 AM.png
    15.9 KB · Views: 459
  • Screen Shot 2018-03-02 at 9.43.58 AM.png
    Screen Shot 2018-03-02 at 9.43.58 AM.png
    4.4 KB · Views: 447
  • ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    15.9 KB · Views: 729
  • ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    4.4 KB · Views: 256
Last edited:
Physics news on Phys.org
  • #2
What is the . doing in this expression?
Code:
(mass.)

Also, it would have been helpful to know which line was line 31 ...

Edit: Furthermore, you cannot let v(i) depend on v(i), you should let it depend on v(i-1) ...
 
  • Like
Likes grandpa2390
  • #3
Orodruin said:
What is the . doing in this expression?
Code:
(mass.)

Also, it would have been helpful to know which line was line 31 ...

Edit: Furthermore, you cannot let v(i) depend on v(i), you should let it depend on v(i-1) ...

I'm sorry, I was thinking that the code would have to be pasted in MatLab. Line 31 is
Code:
velocityWithDrag(i) = velocityWithDrag(i) + ((power.*dt)./(mass.*velocityWithDrag(i)))-((dragCoefficient.*airDensity.*surfaceArea.*velocityWithDrag(i)^2)/(mass.));

I don't know what the period does. I just saw people doing that in similar codes and thought it had to be done.

and for the edit. Oh, the i+1 is missing from my expression. let me fix that.

edit: I took away the period behind mass in that line, and I got this error:
Index exceeds matrix dimensions.

Error in Bicycle (line 31)
velocityWithDrag(i+1) = velocityWithDrag(i) +
((power.*dt)./(mass.*velocityWithDrag(i)))-((dragCoefficient.*airDensity.*surfaceArea.*velocityWithDrag(i)^2)/(mass));

I think I get what you meant. let me change i+1 to i, and i to i-1.
 
  • #5
Orodruin said:
This should be a strong warning sign to you. If you do not know what something does in a code that you need to write, you should check it out and try to understand it.

See https://se.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html for more information.

I don't know what that means.
?temp_hash=95b13e7a18368781a936daa30318359c.png


I get the decimal point. I just figured it did something like force the program to give approximate answers rather than exact. like my calculator.
 

Attachments

  • Screen Shot 2018-03-03 at 7.42.02 AM.png
    Screen Shot 2018-03-03 at 7.42.02 AM.png
    12.6 KB · Views: 464
  • ?temp_hash=95b13e7a18368781a936daa30318359c.png
    ?temp_hash=95b13e7a18368781a936daa30318359c.png
    12.6 KB · Views: 326
  • #6
In which capacity are you using the dot? What is the reason for writing it down?
 
  • #7
Orodruin said:
In which capacity are you using the dot? What is the reason for writing it down?

because the person who wrote the first bit of the code that I am recycling used it.
 
  • #8
Orodruin said:
In which capacity are you using the dot? What is the reason for writing it down?

Code:
%%Velocity Without Drag

t = tmin:dt:tmax; % time steps

[j,k] = size(t);

velocityWithoutDrag(1) = vmin;

% calculate instantenous speeds

for i = 2:k

velocityWithoutDrag(i) = velocityWithoutDrag(i-1) + (power.*dt)./(mass.*velocityWithoutDrag(i-1));

end

I got this part from someone else. and it works. I have to create the second part. add in the line that includes drag. Knowing nothing about how this programming language works, I am having to follow what I see to the best of my ability. In the hopes that I will pass this class (that I shouldn't have taken. If I had known it was going to be programming. But now I am stuck)
 
  • #9
and changing the formula so that v(i) depends on v(i-1) rather than v(i+1) depending on v(i) did the trick. :)
and taking away the . from mass.
in retrospect that seems obvious. except why did the author of the formula right it the way he did?
 
  • #10
@Orodruin
ok, so here I am
Code:
close all

clear all

clc
%%defining variables

tmin = 0; %(in seconds)

tmax = 200; %(in seconds)

dt = .1;

vmin = 1; % in m/s

power = 400; % in Watts

mass = 70; % in kg

dragCoefficient = .5

airDensity = 1.225 %kg/m^3

surfaceArea = .33 %in Meters Squared
%%Velocity Without Drag

t = tmin:dt:tmax; % time steps

[j,k] = size(t);

velocityWithoutDrag(1) = vmin;

% calculate instantenous speeds

for i = 2:k

velocityWithoutDrag(i) = velocityWithoutDrag(i-1) + (power.*dt)./(mass.*velocityWithoutDrag(i-1));

end
%%Velocity With Drag

t = tmin:dt:tmax; % time steps

[j,k] = size(t);

velocityWithDrag(1) = vmin;

% calculate instantenous speeds

for i = 2:k

velocityWithDrag(i) = velocityWithDrag(i-1) + ((power.*dt)./(mass.*velocityWithDrag(i-1)))-((dragCoefficient.*airDensity.*surfaceArea.*velocityWithDrag(i-1)^2)/(mass));

end
%%Plots

figure(1)

hold on

box on

plot(t,velocityWithoutDrag,'-k',t,velocityWithDrag,'--k')

set(gca,'XScale','lin','YScale','lin')

set(gcf,'Color','w');

set(gcf,'Resize','on');

ylabel('velocity (m/s)')

xlabel ('time (s)')

text(t(700),velocityWithoutDrag(10),'With air resistance')

text(t(30),velocityWithoutDrag(700),'No air resistance')

title('Bicycle simulation: velocity vs. time')

hold off
I've corrected the i+1 and i's, and took the dot off of mass. the code is working. But now I need to make it look exactly like the plot. I changed my headings. changed the velocity with drag plot to a dotted line. changed the colors to black.

how do I change the number of ticks. so that the y-axis goes in steps of 10 rather than 5? a google search provides what appears to be a variety of ways. but I can't make sense of them.

edit: I think it works if I change Xscale to Xtick
 
  • #11
Honestly, it appears as if whoever did the code you were given does not know what they are doing either.

Matlab is largely written for the explicit purpose of manipulating matrices (that is where the "mat" in the name comes from). Used this way, the dot in front of an operator signifies that the operation should be performed element by element, i.e., * is matrix multiplication whereas .* is multiplication element by element. In this case, your entire expression handles numbers, i.e., 1x1 matrices, and it therefore does not matter if you have the dots or not.
 
  • #12
Orodruin said:
Honestly, it appears as if whoever did the code you were given does not know what they are doing either.

Matlab is largely written for the explicit purpose of manipulating matrices (that is where the "mat" in the name comes from). Used this way, the dot in front of an operator signifies that the operation should be performed element by element, i.e., * is matrix multiplication whereas .* is multiplication element by element. In this case, your entire expression handles numbers, i.e., 1x1 matrices, and it therefore does not matter if you have the dots or not.

That makes a lot of sense. It gives me the ability to ask an "educated" question (I hope). Why did the dot on the mass at the end of that formula mess it up? but not any of the other dots?
 
  • #13
grandpa2390 said:
Why did the dot on the mass at the end of that formula mess it up?
Because that dot was not part of an element-wise operator. It was just out of place and not in accordance to Matlab syntax.
 

Related to Why won't my Matlab code work?

1. Why am I getting an error message when I try to run my code?

There could be several reasons for this. Some common causes include syntax errors, missing variables, or incorrect function usage. Check your code for any mistakes and make sure all necessary variables are defined before running the code.

2. Why is my code running but not producing the expected output?

This could be due to logical errors in your code. Make sure your program is following the correct logic and that all necessary conditions are being met. Debugging tools such as breakpoints and step-through execution can also help identify the issue.

3. Why is my code taking a long time to run?

Depending on the complexity of your code and the size of your data, it may take longer to run. However, if your code is taking an unusually long time, it could be due to inefficient algorithms or excessive use of nested loops. Consider optimizing your code for better performance.

4. Why is my code producing different results each time I run it?

This could be due to the use of random functions in your code or the data being read in a different order each time. If you want consistent results, try setting a seed for the random functions or sorting the data before using it.

5. Why is my code not working on a different computer or version of Matlab?

There may be compatibility issues with your code and the version of Matlab or operating system on the other computer. Make sure you are using compatible versions and check for any differences in syntax or function usage between versions.

Similar threads

  • Introductory Physics Homework Help
Replies
27
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
Back
Top