Differentiating within a for loop

So i have the following equation, i have to differentiate 3 times using a for loop:
let y =ae^bx sin(cx)
Ive followed a booklet guide i was given and the outputs i have look wrong, theres 2 attemps (images) below, does anyone know where im going wrong
for both images the following has been Initialised
Attempt 1
a = 5, b = 2, c = 4, d = 3
syms x
for y=1:1:3, diff (a*exp(b*x))*sin(c*x),end
ans =
10*sin(4*x)*exp(2*x)
ans =
10*sin(4*x)*exp(2*x)
ans =
10*sin(4*x)*exp(2*x)
Attempt 2
for y=1:1:3, diff a*exp(b*x)*sin(c*x),end
ans =
Columns 1 through 14
-55 59 19 -8 -72 58 -56 78 -79 1 73 -10 5 -70
Columns 15 through 18
59 -57 78 -79
ans =
Columns 1 through 14
-55 59 19 -8 -72 58 -56 78 -79 1 73 -10 5 -70
Columns 15 through 18
59 -57 78 -79
ans =
Columns 1 through 14
-55 59 19 -8 -72 58 -56 78 -79 1 73 -10 5 -70
Columns 15 through 18
59 -57 78 -79

댓글 수: 4

You didn't declare a,b,c,x as symbolic
syms a b c x
James Tursa
James Tursa 2020년 3월 22일
Please post code as text, not as images. We can't copy and run images ...
Ryan
Ryan 2020년 3월 22일
sorry i just changed it
Rik
Rik 2020년 3월 25일
In response to the flag by Ryan ("I would like to delete this question please"):
Why do you want to delete your question? If you weren't allowed to ask for help on your homework you shouldn't have posted it in the first place. In case you decide to edit away all content: don't bother, I made a capture of this page on the Wayback Machine, so there will be a permanent record we can easily restore your edits from.

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2020년 3월 22일
편집: James Tursa 2020년 3월 22일

0 개 추천

I assume by "three times" what was meant was diffferentiate the results iteratively three times, not differentiate the original three times ... what would be the point in that? E.g.,
df{1} = a * exp(b*x) * sin(c*x);
df{2} = diff(df{1});
df{3} = diff(df{2});
df{4} = diff(df{3});
Then put the last three lines in a loop.

댓글 수: 4

Ryan
Ryan 2020년 3월 22일
the question states "Differentiate your equation 3 times in a row using a for loop"
James Tursa
James Tursa 2020년 3월 22일
편집: James Tursa 2020년 3월 22일
Yeah, and I left that little part for you to do. Do you know how to turn those last three lines into a for loop?
Or, if you don't care about the intermediate results, just overwrite f three times. E.g.,
f = your symbolic expression
f = diff(f); % <-- put this line inside a loop
Ryan
Ryan 2020년 3월 22일
편집: Ryan 2020년 3월 22일
I'm really new to this, I'm sorry
i tried the following and got an error in line 1
df{1} = a * exp(b*x) * sin(c*x);
n=3
for df=1:n
df{2} = diff(df{1});
df{3} = diff(df{2});
df{4} = diff(df{3});
end
sprintf ("the first Differentiation is %d", df{1})
sprintf ("the second Differentiation is %d", df{2})
sprintf ("the third Differentiation is %d", df{3})
What error message did you get? Did you include all of the symbolic stuff for x and the a, b, c up front in your code?
You shouldn't use df for both the index variable name and at the same time use it for your derivatives. That's trying to use the same name for two different things, right?
A loop would look like this, where k is the index variable name
for k=2:n
df{k} = diff(df{k-1});
end

댓글을 달려면 로그인하십시오.

제품

릴리스

R2019b

질문:

2020년 3월 22일

댓글:

Rik
2020년 3월 25일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by