Solve for, and then substitute.

3 views (last 30 days)
I posted before, but I am not getting the answers I am looking for. This is a simplified version of what I want to do.
say, f(t)=y*t, and we are given an initial condition f(1)=10. We can use this information to solve for y..
Now that we know, y=10, solve for f(2)=?, obviously f(2) will equal 20.
How can I write this code in MATLAB?
  2 Comments
Kyle Langford
Kyle Langford on 19 Feb 2022
Edited: Walter Roberson on 19 Feb 2022
I am trying to solve this:
given
y(1.2)=80
, and the equation
y(t)==K*A + (y0-K*A)*exp(-t/T)
K=1
A=100
y0=0
y_t=80 %y(1.2)=80
t=1.2
eq1=y_t==K*A + (y0-K*A)*exp(-t/T)
vpasolve(eq1,T)
This gives me that T=0.7456.
Now using knowing T, solve for y(1.5)
Kyle Langford
Kyle Langford on 19 Feb 2022
I can do this, but i was trying but I am trying to find a more creative way to code this.
clear;clc;
syms x
syms
K=1
A=100
y0=0
y_t=80 %y(1.2)=80
t=1.2 %subs for t=1.5
eq1=y_t==K*A + (y0-K*A)*exp(-t/x)
T=vpasolve(eq1,x)
clear eq1
y=@(t) K*A + (y0-K*A)*exp(-t/T)
y(1.5)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Feb 2022
K=1
K = 1
A=100
A = 100
y0=0
y0 = 0
syms y(t) T
y(t) = K*A + (y0-K*A)*exp(-t/T)
y(t) = 
y_known = 80 %y(1.2)=80
y_known = 80
t_known = 1.2
t_known = 1.2000
eq1 = y(t_known) == y_known
eq1 = 
sol_T = solve(eq1, T)
sol_T = 
y(t) = subs(y(t), T, sol_T)
y(t) = 
y(1.5)
ans = 
vpa(ans)
ans = 
86.625193900471559519935338534827

More Answers (1)

Arif Hoq
Arif Hoq on 18 Feb 2022
Edited: Arif Hoq on 18 Feb 2022
you can use anonymous function:
y=10;
f=@(t) y*t;
f(1)
ans = 10
f(2)
ans = 20
  1 Comment
Kyle Langford
Kyle Langford on 19 Feb 2022
Although that does work, that is not what I asked. In this specific scenario, it is easy to figure out y. In a more complex scenario, it would not be so easy.
I think you are on to something, but how could you solve for y given the initial condition f(1)=10, and then additionally solve for f(2) using code?

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!