I am trying to introduce two parameters: initial condition x0 and time step h, so I tried to do what the example from the text file did, but my code crashed. What do I need to do for the code to work. I will post the example codes, the original code, and what I tried .
Ok so here is the example code with NewtonHeat and Newtong Heating %NewtonHeat %script to generate a plot %open the editor with the edit command %copy thecommands below into the editor %save this as a file called NewtonHeat.m clear all theta=15; thetahistory=15; k=50; T=35; c=1; q=800; m=10; Time=2; N=100; h=Time/N; for n=1:N theta=theta+(h/(m*c))*(q-k*(theta-T)); thetahistory=[thetahistory theta]; end t=0:h:Time; plot(t,thetahistory) xlabel('time'), ylabel('body temperature') %axis([0 Time]) WITH PARAMETERS INTRODUCED function [thetalast,thetaequilibrium] = NewtonHeating(theta, k, T, c, q, m, Time) %NewtonHeat computes the heating of a body of initial heat theta % with the parameters k, T, c, q, m, over time interval [0, Time] % The function plots the graph of temperature vs time, % and also outputs thetalast = the temperature at time Time % and thetaequilibrium = the equilibrium temperature. thetahistory=theta; N=100; h=Time/N; for n=1:N theta=theta+(h/(m*c))*(q-k*(theta-T)); thetahistory=[thetahistory theta]; end t=0:h:Time; plot(t,thetahistory) xlabel('time'), ylabel('body temperature') thetalast=theta; thetaequilibrium=q/k+T;
And this is what I'm asked to do % Modify the program degreeday on p. 35 of the textbook so % that it becomes a function with two parameters: the % initial condition (call it x0), and the time step, h. % Try to reproduce the result from the book by calling your % function with parameters 0 and 0.01: % % ddays = degreeday(0,0.01); % % Then try to call the function with initial condition equal % to 0.5. What do you get for the number of days? ORIGINAL CODE function degreeday clear all x=0; maxtime=30; numsteps=100000; h=0.01; for n=1:numsteps if x<1 x=x+h*0.004*(28+5*cos(2*pi*(n-1)*h)-15); else break end end x days=n*h
What I've done so it seems to match what the example did function[InitialCondition,Time]=degreedays(x0,h) clear all x=0; maxtime=30; numsteps=100000; h=0.01; for n=1:numsteps if x<1 x=x+h*0.004*(28+5*cos(2*pi*(n-1)*h)-15); else break end end x; days=n*h; InitialCondition=x0; Time=h;
oh damn it scripts and files must be the same name... ok pretend degreedays is degreeday
Okay, there are a few things to look at: 1. Mathlab functions are declared in the following way ```mathlab function [y1, y2, ...] functionName(x1, x2, ...) ``` you added the arguments correctly but there are no new specification of the output in assignment, so there should be no square brackets. 2. These are the initial values in the code provided by the textbook ``` x=0; maxtime=30; numsteps=100000; h=0.01; ``` What they want you to do in the assignment is to replace 0 and 0.01 with parameters so a caller can pass the initial values instead of them being constant. 3. If you look at the two final lines in the code from the book you'll see there are no semicolons after the expressions. This tells mathlab to print the value of evaluated line in the console/output box so you'll see the actual result of that expression. This is usually good for debugging or when the function doesn't return any value (just like our new function).
no new specifications of the output?
and for 2 should I replace x = x0 for initial condition and h = h for time step?
"no new specifications of the output?" - the assignment tell you to add input parameters but it doesn't tell you anything the function should return, so [InitialCondition,Time] is unnecessary. And if you look at it from a logical perspective why should the function return the values it received in the parameters, the caller would obviously already know these. "and for 2 should I replace x = x0 for initial condition and h = h for time step?" - yes! but h=h doesn't do anything new. If there's already a value assigned to h is there really a need to assign that value to h again?
so I should remove that long thing with the brackets and just have function degreedays(x0,h)?
Not enough input arguments. wtheck?!
how are you calling the function?
line 2 x=x0; maxtime=30; numsteps=100000; h=0.01; not enough input arguments
LIke this? function degreedays(x0,h); x=x0; maxtime=30; numsteps=100000; h=0.01; for n=1:numsteps if x<1 x=x+h*0.004*(28+5*cos(2*pi*(n-1)*h)-15); else break end end x days=n*h;
calling the function?
I keep on getting the same error X>X! Not enough input arguments.. line 2 wtheck
is there something wrong with the code? WHy is line 2 acting like this?
function degreedays(x0,h); x=x0; maxtime=30; numsteps=100000; h=0.01; for n=1:numsteps if x<1 x=x+h*0.004*(28+5*cos(2*pi*(n-1)*h)-15); else break end end x; days=n*h; InitialCondition=x0; Time=h; and every time I run it... line 2 goes haywire and the script windows keeps insisting that I use ;
oh wow I got the error to shut up by assigning a different value of x
function degreedays(x0,h); x=6; maxtime=30; numsteps=100000; h=0.01; for n=1:numsteps if x<1 x=x+h*0.004*(28+5*cos(2*pi*(n-1)*h)-15); else break end end x days=n*h I replaed x0 with 6 and it calculated it x = 6 days = 0.0100
hmm maybe for some reason we need to relabel that x0 since matlab hates it so much, but that label is necessary in the assignment
function degreedays(x0,h); ^ No semicolon here! h=0.01 have to be removed otherwise you'll overwrite the value in (x0, h). I think there's missing an end (you have to close the function) after the final line. When I run it i works fine, so apart from I wrote above I'm not sure if there's more i can do... http://ideone.com/GgCPOy
Correction: otherwise you'll overwrite the value you get from (x0, h)
interesting result >> ddays = degreedays(0,0.01); x = 1.0017 days = 19.2500 Error in degreedays (line 2) x=x0; maxtime=30; numsteps=100000; h=0.05; Output argument "ddays" (and maybe others) not assigned during call to "C:\Users\momo\Documents\MATLAB\degreedays.m>degreedays". So it calculated but at the same this error popped up
ok this is interesting.. I got it to calculate but at the same time I got this error bs. If I could get rid of it that would be the shiz
Did you try what I wrote in my previous post?
going to soon ^_^ but cool I got it to replicate but if only this error would buzz... I'm home free ^___^
yeah I did again it calculated, but at the same time this stupid error message popped up. BTW I had to rename your degreedays to something else to avoid clashes
but it did produce the same calculations as my code.. so it works but the same time the error thing is annoying.
Oh, I think I got it! Don't do ``` ddays = degreedays(0, 0.001) ``` degreedays doesn't return anything so mathlab complains about not being able to assign any value too ddays. Try this instead ``` degreedays(0, 0.001) ```
degreedays(0, 0.01) **
NICE ! :D
but I had to do h=h in my code for it to work.. strange but it calculatesssss!!!!!
AWWW YES!!!!!!!!!!!!!!!!!!!!!!!!!!!
both codes work! HAPPY! :D
Sweet! That feeling when the code is finally working is so great isn't it ;)
yeah even though it can be a royal pain XD!
Join our real-time social learning platform and learn together with your friends!