Why Won't My Matlab Code Work?

Click For Summary
The user encountered an error in their Matlab code related to unexpected parentheses and incorrect indexing. They initially used an incorrect expression that referenced the current index instead of the previous one, which led to an "Index exceeds matrix dimensions" error. After removing an unnecessary period from the variable "mass" and correcting the indexing, the code began to function as intended. The user is now focused on adjusting the plot's appearance, specifically changing line colors and tick intervals on the y-axis. Understanding Matlab's syntax and element-wise operations has been a key part of resolving their coding issues.
grandpa2390
Messages
473
Reaction score
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: 519
  • 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: 508
  • ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    15.9 KB · Views: 828
  • ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    ?temp_hash=4b48d5463545fe8500667f8d77a05e6a.png
    4.4 KB · Views: 348
Last edited:
Physics news on Phys.org
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
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.
 
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: 532
  • ?temp_hash=95b13e7a18368781a936daa30318359c.png
    ?temp_hash=95b13e7a18368781a936daa30318359c.png
    12.6 KB · Views: 390
In which capacity are you using the dot? What is the reason for writing it down?
 
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.
 
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)
 
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.
 

Similar threads

  • · Replies 27 ·
Replies
27
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K